在单独的线程中创建的Tcl Interp会共享任何全局数据吗? [英] Will Tcl Interp created in separate threads share any global data?

查看:125
本文介绍了在单独的线程中创建的Tcl Interp会共享任何全局数据吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的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

Tcl允许您在每个操作系统线程中使用一个或多个Tcl解释器(例如,使用Tcl_CreateInterp()创建).但是,每个解释器都紧密地绑定到其OS线程,如果让多个线程调用同一个解释器(例如,使用Tcl_Eval),则会发生错误.

Tcl lets you have one or more Tcl interpreters (e.g., created with Tcl_CreateInterp()) in each operating system thread. However, each interpreter is tightly bound to its OS thread and errors will occur if you let more than one thread call into the same interpreter (e.g., with Tcl_Eval).

我猜这意味着,如果我不在口译员之间共享数据,那么应该没有问题吗?

I guess that means if I don't shared data between interpreters, then there should be no issue?

推荐答案

我为每个线程创建一个tcl interp,并将其用于Tcl_EvalEx脚本,并通过Tcl_GetStringResult获取结果,这个线程安全吗?

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?

是的. 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会共享任何全局数据吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆