1

For c programming, if i want to coordinate two concurrently executing processes, I can use sleep(). However, i heard that sleep() is not a good idea to implement the orders of events between processes? Are there any reasons?

user605947
  • 119
  • 4
  • 9

4 Answers4

5

sleep() is not a coordination function. It never has been. sleep() makes your process do just that - go to sleep, not running at all for a certain period of time.

You have been misinformed. Perhaps your source was referring to what is known as a backoff after an acquisition of a lock fails, in which case a randomized sleep may be appropriate.

The way one generally establishes a relative event ordering between processes (ie, creates a happens-before edge) is to use a concurrency-control structure such as a condition variable which is only raised at a certain point, or a more-obtuse barrier which causes each thread hitting it to wait until all others have also reached that point in the program.

Borealid
  • 95,191
  • 9
  • 106
  • 122
4

Using sleep() will impact the latency and CPU load. Let's say you sleep for 1ms and check some atomic shared variable. The average latency will be (at least) 0.5ms. You will be consuming CPU cycles in this non-active thread to poll the shared atomic variable. There are also often no guarantees about the sleep time.

The OS provides services to communicate/synchronize between threads/processes. Those have low latency, consume less CPU cycles, and often have other guarantees - those are the ones you should use... (E.g. condition variables, events, semaphores etc.). When you use those the thread/process does not need to "poll". The kernel wakes up the waiting threads/processes when needed (the thread/process "blocks").

There are some rare situations where polling is the best solution for thread/process synchronization, e.g. a spinlock, usually when the overhead of going through the kernel is larger than the time spent polling.

Community
  • 1
  • 1
Guy Sirton
  • 8,331
  • 2
  • 26
  • 36
0

In my case, I was using this function in celery. I was doing time.sleep(10). And it was working fine if the celery_task was called once or twice per minute. But it created chaos in one case.

  • If the celery_task is called 1000 times
  • I had 4 celery workers, so the above 1000 celery calls were queued for execution.
  • The first 4 calls were executed by the 4 workers and the remaining 996 were still in the queue.
  • the workers were busy in the 4 tasks for 10 seconds and after 10 secs it took the next 4 tasks. Going this way it may take around 1000\4*10=2500 seconds.

Eventually, we had to remove time.sleep as it was blocking the worker for 10 seconds in my case.

Vikram Ray
  • 960
  • 2
  • 10
  • 17
0

Sleep would not be a very robust way to handle event ordering between processes as there are so many things that can go wrong. What if your sleep() is interrupted? You need to be a bit more specific about what you mean by "implement the order of events between processes".

John3136
  • 28,809
  • 4
  • 51
  • 69