Dipro Sen
Dipro Sen

Reputation: 4670

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

Upvotes: 1

Views: 467

Answers (4)

jxh
jxh

Reputation: 70382

I don't know of a way for a thread to forcibly cause another thread to sleep. If two threads are accessing a shared resource (like an input/output queue, which seems likely for you Produce/Consumer example), then both threads may contend for the same lock. The losing thread must wait for the other thread to release the lock if the contention is not the "trylock" variety. The thread that waits is placed into a waiting queue associated with the lock, and is removed from the schedulers run queue. When the winning thread releases the lock, the code checks the queue to see if there are threads still waiting to acquire it. If there are, one is chosen as the winner and is given the lock, and placed in the scheduler run queue.

Upvotes: 0

Tony The Lion
Tony The Lion

Reputation: 63190

The thread that sleep is invoked on is put in the idle queue by the thread scheduler and is context switched out of the CPU it is running on, so other threads can take it's place.

All context (registers, stack pointer, base pointer, etc) are saved on the thread stack, so when it's run next time, it can continue from where it left off.

The OS is constantly doing context switches between threads in order to make your system seem like it's doing multiple things. The OS thread scheduler algorithm takes care of that.

Thread scheduling and threading is a big subject, if you want to really understand it, I suggest you start reading up on it. :)

EDIT: Using sleep for thread synchronization purposes not advised, you should use proper synchronization mechanisms to tell the thread to wait for other threads, etc.

Upvotes: 3

Tudor
Tudor

Reputation: 62439

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.

Upvotes: 0

Rook
Rook

Reputation: 6145

It would be unadvisable for one thread to forcibly and synchronously interfere with the execution of another thread. One thread could send an asynchronous message to another requesting that it reschedule itself in some way, but that would be handled by the other thread when it was in a suitable state to do so.

Assuming they communicate using channels that are thread-safe, nothing bad shoudl happen, as the sleeping thread will wake up eventually and grab data from its task queue or see that some semaphore has been set and read the prodced data.

If the threads communicate using nonvolatile variables or direct function calls that change state, that's when Bad Things occur.

Upvotes: 0

Related Questions