QtScript 和线程
我想从 QtScript 脚本运行多个并发作业:
function job1() { ... }
function job2() { ... }
runConcurrentJobs(job1, job2)
这些作业本质上是远程过程调用(ZeroC Ice)的序列,需要在多个点同步。
Qt 4.8.0 文档没有提及 QScriptEngine 线程安全性。我的问题:
使用单个
QScriptEngine
从多个线程同时执行 QtScript 函数是否安全?您建议采用什么方法来完成任务?
注释:
- 脚本不是由程序员编辑,而是由电气工程师编辑,我希望使脚本尽可能简单和干净。
I want to run several concurrent jobs from QtScript script:
function job1() { ... }
function job2() { ... }
runConcurrentJobs(job1, job2)
The jobs a essentially sequences of remote procedure calls (ZeroC Ice), which need to synchronize at several points.
The Qt 4.8.0 documentation says nothing about QScriptEngine
thread-safety. My questions:
Is it safe to use single
QScriptEngine
to exectute QtScript functions from multiple thread concurrently?What approach would you recommend to accomplish the task?
Notes:
- Scripts are edited not by programmers but also by electric engineers and I want to keep the script as simple and clean as possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
QScriptEngine
被记录为“可重入”,这意味着,本质上,您可以多线程使用它,但每个线程只能使用一个QScriptEngine
。现在,如果函数
job1()
和job2()
可以同时运行,原则上,应该可以将它们分成两个不同的QScriptEngine< /code>s (简单,如果函数都不使用局部变量,则更困难,如果涉及全局变量)。
runConcurrentJobs()
实现为Q_INVOKABLE
函数(或槽)。在那里,做类似的事情
MyScriptThread 源自 QThread 并实现 QThread::run() 的操作,大致如下:
QScriptEngine
is documented as "reentrant", meaning, essentially, you can use it multi-threaded, but only oneQScriptEngine
per thread.Now, if functions
job1()
andjob2()
can be run concurrently at all, in principle, it should be possible to separate them into two distinctQScriptEngine
s (easy, if neither the functions use local variables, only, more difficult, if globals are involved).runConcurrentJobs()
as aQ_INVOKABLE
function (or slot) in C++.In there, do something like
Where MyScriptThread is derived from QThread and implements QThread::run() roughly like this:
一般来说,如果文档没有提及线程,那么它就不是线程安全的。
我会重写以使用异步请求。把他们俩踢开,然后等他们俩。
In general, if documentation says nothing about threading, it is not thread safe.
I would rewrite to use asynchronous requests. Just kick them both off, then wait for them both.