Solved How to put the thread to sleep before the desired signal appears?
-
I created an instance of QThread to perform asynchronous actions from the main threads. This object has two signals:
void finished() void started()
started() it is necessary to wait before the start of the main threads, and finished() - before creating the message about the end of processing.
Of course, I can connect each of these signals to the slots using connect(). But it strongly disfigures the code (it is necessary to put all the main code in one slot, and in the other slot to put the code of the completion of processing). The code turns out to be grouped not by functionality, but by "signal-slot" necessity ...
Is there any method that "sleep" the current thread until the desired signal appears?
-
@AlekseyB said in How to put the thread to sleep before the desired signal appears?:
started() it is necessary to wait before the start of the main threads, and finished() - before creating the message about the end of processing.
why, and why? How did you implement the threading, did you override
QThread::run
?The code turns out to be grouped not by functionality, but by "signal-slot" necessity ...
Which is how event-driven programming works to begin with.
Is there any method that "sleep" the current thread until the desired signal appears?
Depends on what is supposed to sleep until which signal appears ... could you elaborate?
-
@kshegunov said in How to put the thread to sleep before the desired signal appears?:
elaborate
I need the thread for auxiliary purposes in order to remove long-term operations from the main threads. Before creating main threads, I need to make sure that exist a thread that getting their's tasks.
At the end of the work of the main threads, I need to make sure that the auxiliary thread also terminated.
I do not want ancillary tools to specify the logic for building an application.
Now in the body of the run(), I have only started the loop for processing signals and messages:
void run() { exec(); }
-
I need the thread for auxiliary purposes in order to remove long-term operations from the main threads.
Yeah, yeah, that's what's the purpose of threads to begin with.
Before creating main threads, I need to make sure that exist a thread that getting their's tasks.
Again, why? You can start the thread at any time you need, there's no need to be so very specific about the exact time (i.e. before
main()
).At the end of the work of the main threads, I need to make sure that the auxiliary thread also terminated.
That's why you have
QThread::wait
. Simple example follows:int main(int argc, char **argv) { QApplication app(argc, argv); QThread thread; thread.start(); QObject::connect(&app, &QCoreApplication::aboutToQuit, &thread, &QThread::quit); // Do other stuff. // ... stuff int returnValue = QApplication::exec(); thread.wait(); return returnValue; }
Now in the body of the run(), I have only started the loop for processing signals and messages
For what reason? This is what the default implementation does, so there's no need to override the run method at all.
-
@kshegunov said in How to put the thread to sleep before the desired signal appears?:
thread.wait()
Thank you!
As I understood, there is a blocking method for checking the end of the thread, but there is no method for checking its successful start. In the second case, I can use:while(!thread.isRunning ()) yieldCurrentThread ();
-
You can use a semaphore:
QThread thread; QObject worker; worker.moveToThread(&thread); QSemaphore semaphore; QObject::connect(&thread, QThread::started, &worker, [&semaphore] () -> void { semaphore.release(); }); thread.start(); if (!semaphore.tryAcquire(1, 5000)) { // Semaphore was not released in 5 seconds } // Continue on ...
... but I continue to puzzle over why would you want to block the main thread to begin with ...
-
@kshegunov said in How to put the thread to sleep before the desired signal appears?:
but I continue to puzzle over why would you want to block the main thread to begin with
"I need the thread for auxiliary purposes in order to remove long-term operations from the main threads. Before creating main threads, I need to make sure that exist a thread that getting their's tasks."