问题描述
在我的C ++代码中,如果我为每个线程创建一个tcl interp,并将其用于Tcl_EvalEx脚本,并通过Tcl_GetStringResult获取结果,那么此线程安全吗?
In my C++ code, if I create one tcl interp per thread, and use it to Tcl_EvalEx a script, and get result by Tcl_GetStringResult, is this thread safe?
除了const数据外,这些线程之间没有共享数据.
There's no shared data between these threads except const data.
在Google上进行一些搜索后,我在tcl线程模型文档中找到了这个: http://www.tcl.tk/doc/howto/thread_model.html
After some searching on google I found this in the tcl threading model doc:http://www.tcl.tk/doc/howto/thread_model.html
我猜这意味着,如果我不在口译员之间共享数据,那么应该没有问题吗?
I guess that means if I don't shared data between interpreters, then there should be no issue?
推荐答案
是的. Tcl的引擎广泛使用特定于线程的数据,因此不可能在线程之间传输解释器(事情很糟糕),但是您有同时保证线程安全的好处. .要记住的主要事情是在与Tcl的实现混合在一起时完全使用Tcl的内置内存分配器函数,而不是自己尝试做,因为线程Tcl使用的关键之一是线程感知的内存分配器( 更快.
Yes. Tcl's engine uses thread-specific data extensively, so transferring an interpreter between threads is impossible (things break horribly), but you have the up-side that thread-safety is guaranteed at the same time. The main thing to remember is to use Tcl's built-in memory allocator functions when intermingling with Tcl's implementation at all instead of trying to do it yourself, as one of the key things that threaded Tcl uses is a thread-aware memory allocator (much faster, given that most data stays bound to a single thread).
简单的代码确实可以保持简单.很好,是吗?
Simple code can, indeed, stay simple. Nice, yes?
好吧,如果您做一些技巧,例如构建非线程感知版本等,这是可能的,但这确实很棘手,对于深层次的专家来说,这仅是潜在的灾难性问题真的很高. Tcl的高度分区模型非常容易使用.
OK, it's possible if you do tricks like building a non-thread-aware version and so on, but it's really tricky and one for deep experts only as the potential for catastrophic problems is really high. Tcl's highly partitioned model is hugely easier to work with.
这篇关于在单独的线程中创建的Tcl Interp会共享任何全局数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!