How do I put to sleep a thread other than the current one.

  • Thread starter Thread starter Peter Rilling
  • Start date Start date
P

Peter Rilling

How do I put to sleep a thread other than the current one?

I want to put another thread to sleep to the specified number of
milliseconds but the Sleep method seems to only work for the current thread
while the Suspend method has no parameters to specify a duration.
 
I want to put another thread to sleep to the specified number of
milliseconds but the Sleep method seems to only work for the current thread
while the Suspend method has no parameters to specify a duration.

I am afraid that this is not possible at all. for the thread to sleep it
must be active at the moment you invoke the sleep function (otherwise it is
senseless because the thead IS sleeping!).

what you want to do is not to call Sleep for the other thread but rather to
block its execution for some time. to accomplish that you should use any
object that can synchronize threads, for example Mutexes.

Regards,
Wiktor Zychla
 
I am afraid that this is not possible at all. for the thread to sleep it
must be active at the moment you invoke the sleep function (otherwise it is
senseless because the thead IS sleeping!).

What if I have a dual processor? Can't processor 1 and 2 be running a
different thread at the same time?
 
Rene said:
What if I have a dual processor? Can't processor 1 and 2 be running a
different thread at the same time?
Sure, but you still need to use the correct synchronization mechanism. In
almost all cases you should use a sync object, e.g. ManualResetEvent, and
the thread blocks on that object until some other thread sets the object to
the signalled state, rather then directly Suspend/Resume a thread.
 
Back
Top