Hatems
Hatems

Reputation: 39

What is the purpose of the sleep() function in a Thread?

I often see Sleep(N) after a thread starts or sometimes I see Thread::Sleep(N); where N is in milliseconds. Is it meant to put the current thread in sleep so that another thread can start?

I appreciate any response

Upvotes: 2

Views: 5220

Answers (6)

Yves
Yves

Reputation: 12371

As I know, we use sleep() for the cases below:

1) Simulation. When you need simulate some situation to test your code, you may use sleep().
For example, you are designing a module, which is to be a server. Now you need to test your server with a case where a client sends a heavy request of 5 sec. To do this test, you don't need a real client. What you just need is to simulate a client with sleep(5000).

2) Give other threads chance of execution --- as you mentioned. But please attention: sleep() will hold the lock.

3) Save the resource of CPU.
For example, in the mode of non-blocking of socket, you may code like this:

while(true)
{
    sleep(200);
    res = accept(mysocket, NONBLOCKING);
    if (getMsg(res))
    {
        // do something
    }
    else
    {
        // do something
    }
}

This is non-blocking mode, when it executes accept, it will check if there is some messages immediately. If no message, it continue.
In this case, if we don't add a sleep() like above, this code will consume lots of resource of CPU for nothing (just a infinite loop). So we add a sleep() so that other threads or other processes can use CPU. In other words, CPU is used more efficient now.
By the way, the network card has its own cache, so if a client sends a message to the code above and at the same time, the code starts to sleep for 1 second, it wouldn't be a problem because after 1 second, the message is still there (in the cache), so the code could get it. But, if in this 1 second, there lots of clients sending messages, the code can miss the messages because 1 second is too long to process messages fast. In a word, you must make sure that the received messages can't fill up the cache in 1 second. Otherwise, sleep shorter or process messages faster.

Upvotes: 2

user5196825
user5196825

Reputation:

Sleep(0) will give up the current time slice to another process if there is a process with a greater priority which is ready to schedule. If there is no process to schedule the current process will return immediately.
Sleep(n) where n > 0 will give up unconditionally the current time slice to another process.

reference

Upvotes: 1

skrtbhtngr
skrtbhtngr

Reputation: 2251

Thread sleep functions are usually meant to block a particular thread's execution for a at least the provided time, usually, in order to let other threads run without any kind of "interference" from it. It may block for more than the provided time due to scheduling or resource contention delays. It does not, however make the sleeping thread release its locks, if any! They are frequently used for debugging purposes.

Upvotes: 0

Doing Thread::Sleep(N); will only delays the program's execution, normally waiting for external resources to be there... this was a very common way in low-level/old languages like Asm, where no OOP was able and where programs where run sequentially. Nowadays are interfaces, callbacks, multi thread, notifications. and is just a bad habit to use such techniques...

Upvotes: 0

SergeyA
SergeyA

Reputation: 62563

Use of sleep() function (and it's friends) usually indicate design flaw. The rare exceptions are sleeps used in debugging.

The common misguided usages of sleep include an attempt to time events to a certain time (bad, because no one guarantees that sleep will take exactly that many units as prescribed on non-RT systems), attempt to wait for some events (bad, because to wait for event you should use specific waiting functions available with your threading library) or an attempt to yield resources - bad, because if you have nothing to do, just exit the thread.

Upvotes: 8

Lie Ryan
Lie Ryan

Reputation: 64837

Sleep puts the thread in non-runnable state for the specified amount of time or until the process is woken up by a signal.

When a thread is in non-runnable state, the OS scheduler won't schedule the thread into the run queue, and the OS forces a thread/context switch so that another runnable task (thread/process) can run instead.

Upvotes: 3

Related Questions