标题:QTimer是线程安全的么?
作者:benkei
日期:2016-04-16 15:59
内容:
最近做项目,里面用了多线程。发现主进程里开的几个qtimer时控经常无故中断,不响应了。
以前没做过多线程的程序,不知道哪位老大有碰到过类似的问题,恳请指导一二。
#1 [uidab 04-18 07:59]
估计是其他问题,Qt自己的帮助说多线程里用QTimer是没问题的。
In multithreaded applications, you can use QTimer in any thread that has an event loop. To start an event loop from a non-GUI thread, use QThread::exec(). Qt uses the timer's thread affinity to determine which thread will emit the timeout() signal. Because of this, you must start and stop the timer in its thread; it is not possible to start a timer from another thread.
何况你只是在主线程里用。
#2 [dbzhang800 04-18 12:52]
QTimer 不是线程安全的,但它是可重入的。所以像uidab所说,在多线程程序中的线程中使用是没有问题的。
#3 回 dbzhang800 的帖子 [stlcours 04-18 23:51]
dbzhang800:QTimer 不是线程安全的,但它是可重入的。所以像uidab所说,在多线程程序中的线程中使用是没有问题的。 (2016-04-18 12:52)
请问线程安全和可重入有什么区别呢?
#4 回 stlcours 的帖子 [smallcsduck 04-19 23:53]
stlcours:请问线程安全和可重入有什么区别呢? (2016-04-18 23:51)
可重入就是类里面没有静态的变量
线程安全就是考虑了多线程下修改变量的问题
#5 [jxgyzhang 04-21 08:11]
QTimer 在哪个线程new起来的这个对象就属于哪个线程,只能在那个线程中进行start、stop、还有析构。如果其他的线程需要对他进行stop或者start操作,只能用信号槽。