Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Thread.Sleep(timeout) and ManualResetEvent.Wait(timeout)?

Both Thread.Sleep(timeout) and resetEvent.Wait(timeout) cause execution to pause for at least timeout milliseconds, so is there a difference between them? I know that Thread.Sleep causes the thread to give up the remainder of its time slice, thus possibly resulting in a sleep that lasts far longer than asked for. Does the Wait(timeout) method of a ManualResetEvent object have the same problem?

Edit: I'm aware that a ManualResetEvent's main point is to be signaled from another thread - right now I'm only concerned with the case of an event's Wait method with a timeout specified, and no other callers setting the event. I want to know whether it's more reliable to awaken on-time than Thread.Sleep

like image 724
Erik Forbes Avatar asked Jun 08 '10 16:06

Erik Forbes


People also ask

What is the difference between thread sleep and wait?

Sleep() method belongs to Thread class. Wait() method releases lock during Synchronization. Sleep() method does not release the lock on object during Synchronization. Wait() should be called only from Synchronized context.

What is difference between sleep () and interrupt () method in Java?

You use interrupt() when you want to interrupt() another Thread which may be waiting on something. You use sleep() when you want this thread to pause for a bit.

What is the difference between wait sleep and yield in Java?

Difference between yield and sleep in Java. The major difference between yield and sleep in Java is that yield() method pauses the currently executing thread temporarily for giving a chance to the remaining waiting threads of the same priority to execute.

What is the difference between sleep and wait Mcq?

Whenever a thread calls wait() method, it releases the lock or monitor it holds and when it calls sleep() method, it doesn't release the lock or monitor it holds. This is the main difference between wait() and sleep() methods.


3 Answers

Thread.Sleep(timeout) causes an unconditional wait before execution is resumed. resetEvent.WaitOne(timeout) causes the thread to wait until either (1) the event is triggered, or (2) the timeout is reached.

The point of using events is to trigger them from another thread, so you can directly control when the thread wakes up. If you don't need this, you shouldn't be using event objects.

EDIT: Timing-wise, they are both equally reliable. However, your comment about "awakening on time" worries me. Why do you need your code to wake up on time? Sleep and WaitOne aren't really designed with precision in mind.

Only if timeout is below 50ms or so and you need the reliability, you should look into alternate methods of timing. This article looks like a pretty good overview.

like image 85
zildjohn01 Avatar answered Nov 15 '22 13:11

zildjohn01


The main difference between Thread.Sleep and ManualResetEvent.WaitOne is that you can signal to a thread waiting on a ManualResetEvent using the Set method, causing the thread to wake up earlier than the timeout.

If you don't signal then I would expect them to behave in a very similar way.

From .NET Reflector I can see that the method ManualResetEvent.WaitOne eventually results in a call to an extern method with the following signature:

int WaitOneNative(SafeWaitHandle waitHandle,
                  uint millisecondsTimeout,
                  bool hasThreadAffinity,
                  bool exitContext);

Whereas Thread.Sleep calls this extern method:

void SleepInternal(int millisecondsTimeout);

Unfortunately I don't have the source code for these methods, so I can only guess. I'd imagine that in both calls result in the thread getting scheduled out while it is waiting for the time out to expire, with neither being particularly more accurate than the other.

like image 39
Mark Byers Avatar answered Nov 15 '22 15:11

Mark Byers


For delays and periodics I have found Monitor.Wait a good choice..

object timelock = new object();

lock (timelock) { Monitor.Wait(timelock, TimeSpan.FromMilliseconds(X.XX)); }

This gives a excellent result....~1ms jitter or better depending on application specifics.

As you may already know Thread.Sleep(X) is unreliable and cannot be canceled....I avoid it like the plague.

like image 33
Rusty Avatar answered Nov 15 '22 15:11

Rusty