The simple answer is: you should not block the asynchronous start code with completion - each event handler and slot implementation in QObject should do its job and return as soon as possible. He should not do any lively or sleeping. For more information on this line, see Miro Samek. I do not like RTOS files .
For a more efficient implementation, which follows from the above, see instead . The macro that follows is best left to poor souls stuck with C.
I have attached an example of how to do this correctly, at least in terms of what the code does. If you want to do a real implementation, look no further than Boost uncouch coroutines .
Macro addition - syntactic sugar - this makes the technique more acceptable (Boost does it better than I do below). Whether you use macros or write methods explicitly is up to you. The syntax is not what is claimed to be the "right way." I am not the only one who uses such a preprocessor blende . There is no support for nested function calls and several "threads" to complete execution until completion in QObject . This example shows the code for only one "thread" and only one level of calls to the asynchronous call function. Stackless Python brings this to its logical conclusion.
You will see this template in all your code if you write it in an asynchronous way. The SLEEP macro is syntactic sugar that helps make code simpler. There is no truly clean way to write it without a hacker macro in C ++, where the syntax will not be overbearing. Even with C ++ 11, the language does not have built-in output support. See Why in C ++ 0x? no revenue has been added.
This is really non-blocking code, you will see that the periodic timer event fires while you are "sleeping". Note that this collaborative multitasking has much lower overhead than threads / processes performed by the OS. There was a reason that the 16-bit code of a Windows application was written as follows: it works pretty well even on meager equipment.
Note that QThread not needed for this code, and in fact, it does not use QThread , although if you move the object to a thread with a high priority, the delays will have a lower spread.
The implementation of the Qt timer is smart enough to reduce the timer period on Windows if the period is "short." You can use the platform-specific code, which I will show below, but this should be discouraged. On Qt 5, you simply start the Qt::PreciseTimer . Note that on systems prior to Windows 8, you trade on power consumption and are slightly higher than kernel overhead for performance here. Windows 8, OS X (xnu), and modern Linux are carefree and do not suffer from such performance degradation.
I must admit the clear direction of preprocessor abuse from creating the C # C ## and __LINE__ macros (combining markers using a positioning macro) .
Similar to the SLEEP() macro, you can also implement the GOTO() macro to allow you to have simple machines with an end state that are written in the style of a simple lock code, but are still asynchronous behind the scenes. You can have the ENTER() and LEAVE() macros to implement the actions that must be performed when entering and exiting the state, etc., But the code may look completely like a direct encoding function with locking. I found it quite productive and lighter than code that lacks syntactic sugar coverage. YMMV. In the end, you will have something that is on the way to UML state maps, but at a lower cost (both at runtime and in text) than QStateMachine-based .
Below is the conclusion, stars are periodic ticks.
doing something * * * * * * * * * * slept, a=10 * * * * * slept, a=20 * * slept, a=30 * slept, a=40
//main.cpp #ifdef Q_WS_WIN #include <windows.h> #endif #include <cstdio> #include <QtCore/QTextStream>