Why can I start a timer only within its associated thread?
In the Qtimer source analysis (take Windows implementation as an example), we talk about:
The Qtimer is implemented through Qobject's timerevent (), and the turn-on and turn-off timers are done through Qobject Starttimer () and KillTimer.
Starttimer eventually invokes the eventdispatcher of the object's associated thread to register the timer:
int qobject::starttimer (int interval) { q_d (qobject); Return D->threaddata->eventdispatcher->registertimer (interval, this);
Under the Win32 platform:
void Qeventdispatcherwin32::registertimer (int timerid, int interval, qobject *object) { if (Timerid < 1 | | interval < 0 | | !object) { qwarning ("qeventdispatcherwin32::registertimer:invalid arguments"); return; } else if (object->thread ()! = Thread () | | thread ()! = Qthread::currentthread ()) { qwarning ("Qobject::starttimer: Timers cannot is started from another thread "); return; } ...
Under the Linux platform:
void Qeventdispatcherglib::registertimer (int timerid, int interval, qobject *object) {#ifndef qt_no_debug if ( Timerid < 1 | | Interval < 0 | | !object) { qwarning ("qeventdispatcherglib::registertimer:invalid arguments"); return; } else if (object->thread ()! = Thread () | | thread ()! = Qthread::currentthread ()) { qwarning ("Qobject::starttimer: Timers cannot is started from another thread "); return; } ...
Under these two platforms, it checks whether the threads of the current thread and dispatcher are consistent. Inconsistencies are returned directly.
Why do you design this? I'm not quite sure. Perhaps because: The callback function is used to register the timer, and the callback function needs to be executed on the registered thread (fix me).
http://blog.csdn.net/lynfam/article/details/7081545
Why can I start a timer only within its associated thread? (QT checks for consistency, otherwise it will not execute)