QThread moveToThread.QThread 中的同步
QThread moveToThread. Synchronization in QThread
我想写一些必须在自己的线程中工作的类。我读过这篇文章:http://wiki.qt.io/Threads_Events_QObjects
.它建议移动必须在自己的线程中工作的对象,例如:
TestClass tst;
QThread *thread = new QThread();
tst.moveToThread(thread);
thread->start();
QObject::connect(thread, SIGNAL(started()), &tst, SLOT(start()));
在测试类的slot
,我放置了所有初始化过程。1. 我可以在测试类的构造函数中移动到线程吗?喜欢:
TestClass::TestClass() {
QThread *thread = new QThread();
this->moveToThread(thread);
thread->start();
QObject::connect(thread, SIGNAL(started()), this, SLOT(start()));
}
之后,此类的所有对象都将在自己的线程中工作。
TestClass
我有私人struct
可以在两个线程中更改。我应该为此使用mutex
还是使用信号/插槽:void TestClass::changeStruct(int newValue) { // invoked in main thread emit this->changeValue(newValue); } // slot void TestClass::changeStructSlot(int newValue) { // this slot will be invoked in the second thread this._struct.val = newValue; }
-
至少从设计的角度来看,我不会这样做。除了
TestClass
应该做什么之外,您还尝试添加内部线程管理。此外TestClass
析构函数也会因为线程管理而变得有点复杂。 -
每个
TestClass
对象都有自己的struct
。如果来自主线程的调用是更改val
的唯一方法,则无需执行任何操作。如果val
可以从多个线程(包括其自己的线程)更改,则使用QMutex
。
相关文章:
- 如何监控QThread
- 松弛原子与无同步情况下的记忆连贯性
- 使用QQuickFramebufferObject时同步数据的最佳方式是什么
- 以线程安全的方式调用"QQuickPaintedItem::updateImage(const QImage&image)"(no QThread)
- 在C++中同步线程
- 与 stdio 同步是否使程序 I/O 非交互式?
- 如何在qt中同步应用程序和显示器的刷新率?
- Windows 进程间同步类似事件?
- 当对套接字 send() 的同步调用由于连接另一端丢失而被阻止时,如何恢复?
- 将 10 个线程与原子布尔值同步
- ofstream::close() 是否在 Linux 上调用同步?
- 与Visual Studios(c ++)同步时如何组织github存储库?
- 等待 qthread 终止的正确方法是什么?
- 如何正确停止运行非循环QThread?
- 原子获取是否与互斥锁释放同步?
- Boost (Beast) websocket:同步写入挂起
- Postgres vs MySQL:命令不同步;.
- Valgrind 在 QThread::start() 上报告内存泄漏
- QThread moveToThread.QThread 中的同步
- Qt同步原语可以与非QThread线程一起使用吗