Why can I start a timer only within its associated thread? (QT checks for consistency, otherwise it will not execute)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.