Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread sleep VS wait condition

EDIT :

The point here is to know the more efficient solution for implement a wait loop which poll a condition on every iteration. By efficient, I mean "efficient for CPU scheduling".

I know that the wait condition used in my code is not a "real wait condition" used witch a "wakeOne"/"wakeAll" instruction but I want to know if using a fake wait condition is more effective for the CPU than a sleep.

Original post :

Here is 2 code snippets, which does the same thing : wait until something happen. This code is used in a pool of worker threads. So when a thread wait, the others (or some of the others) should process their instructions.

The first use a "sleep", the second a "wait condition". They are written with Qt, but could be easily translated to C++11, Boost or any thread library.

The both works well, but is there any performance difference ? I remember that I've read somewhere :

The "sleep" cause an active wait, so the CPU spend time to wait.

The "wait condition" make the CPU wait for an event, so the CPU switch to another thread execution during the wait

Do I remember well ? Is that true ? Using a wait condition is more effective to execute multiple threads in parrallel ?

The "sleep" version :

while (someCondition == false)
{
    sleep(100);
}

// Do some work

The "WaitCondition" version :

QMutex mutex(QMutex::NonRecursive);
QWaitCondition waitCondition;

while (someCondition == false)
{
    QMutexLocker locker(&mutex);
    waitCondition.wait(&mutex, 100);
}

// Do some work
like image 571
Aurelien Avatar asked Feb 14 '17 09:02

Aurelien


People also ask

Why thread sleep is not recommended?

Thread. sleep is bad! It blocks the current thread and renders it unusable for further work.

What is the difference between yield () wait () and sleep ()?

Sleep causes thread to suspend itself for x milliseconds while yield suspends the thread and immediately moves it to the ready queue (the queue which the CPU uses to run threads).

Is thread sleep a good practice?

Using Thread. sleep() frequently in an automation framework is not a good practice. If the applied sleep is of 5 secs and the web element is displayed in 2 secs only, the extra 3 secs will increase the execution time. And if you use it more often in the framework, the execution time would increase drastically.

What is sleep and wait method?

Wait and Sleep are the methods used to pause a process for few seconds and go a thread in the waiting state, respectively. Let's understand both of them one by one to get more information about them.


1 Answers

EDITED:

Both versions are identical, as both "block" or "suspend" (that is - remove it from the "runnable" list in scheduler) the calling thread.

http://pubs.opengroup.org/onlinepubs/9699919799/functions/sleep.html http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_timedwait.html

During sleep and wait for CV the thread does not use any clock cycles of the CPU.

ORIGINAL:

The difference is significant. The first example (with sleep) will react to an event within 0-100ms. The other - with condition variable - will react "instantly". That is because the sleep will do just that - sleep for the exact amount of time you requested (note - let's assume that your system does not use POSIX-style signals). On the other hand, condition variable will wait for as long as you requested unless it receives a notification that the condition might have changed.

like image 176
Freddie Chopin Avatar answered Sep 28 '22 18:09

Freddie Chopin