Search code examples
c++multithreadingboostboost-thread

sleeping a thread in the middle of execution


What happens when a thread is put to sleep by other thread, possible by main thread, in the middle of its execution?

assuming I've a function Producer. What if Consumer sleep()s the Producer in the middle of production of one unit ?

Suppose the unit is half produced. and then its put on sleep(). The integrity of system may be in a problem


Solution

  • There is no problem associated with this, unless some state is mutated while the thread sleeps, so it wakes up with a different set of values than before going to sleep.

    Threads are switched in and out of execution by the CPU all the time, but that does not affect the overall outcome of their execution, assuming no data races or other bugs are present.