Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread.Sleep for less than 1 millisecond

I want to call thread sleep with less than 1 millisecond. I read that neither thread.Sleep nor Windows-OS support that.

What's the solution for that?

For all those who wonder why I need this: I'm doing a stress test, and want to know how many messages my module can handle per second. So my code is:

 // Set the relative part of Second hat will be allocated for each message   //For example: 5 messages - every message will get 200 miliseconds   var quantum = 1000 / numOfMessages;   for (var i = 0; i < numOfMessages; i++)  {       _bus.Publish(new MyMessage());       if (rate != 0)            Thread.Sleep(quantum);  } 

I'll be glad to get your opinion on that.

like image 591
Erez Avatar asked Jun 06 '11 15:06

Erez


People also ask

How do I make my thread sleep for 1 minute?

To make a thread sleep for 1 minute, you do something like this: TimeUnit. MINUTES. sleep(1);

How long should a thread sleep?

If it is a UI worker thread, as long as they have some kind of progress indicator, anywhere up to half a second should be good enough. The UI should be responsive during the operation since its a background thread and you definitely have enough CPU time available to check every 500 ms.

How long is thread sleep 1000?

For example, with thread. sleep(1000), you intended 1,000 milliseconds, but it could potentially sleep for more than 1,000 milliseconds too as it waits for its turn in the scheduler. Each thread has its own use of CPU and virtual memory.

Why thread sleep is not recommended?

If given a wait of 5000 Milliseconds(5 seconds) and an element just take just 1-2 seconds to load, script will still wait for another 3 seconds which is bad as it is unnecessarily increasing the execution time. So thread. sleep() increases the execution time in cases where elements are loaded in no due time.


2 Answers

You can't do this. A single sleep call will typically block for far longer than a millisecond (it's OS and system dependent, but in my experience, Thread.Sleep(1) tends to block for somewhere between 12-15ms).

Windows, in general, is not designed as a real-time operating system. This type of control is typically impossible to achieve on normal (desktop/server) versions of Windows.

The closest you can get is typically to spin and eat CPU cycles until you've achieved the wait time you want (measured with a high performance counter). This, however, is pretty awful - you'll eat up an entire CPU, and even then, you'll likely get preempted by the OS at times and effectively "sleep" for longer than 1ms...

like image 152
Reed Copsey Avatar answered Sep 20 '22 01:09

Reed Copsey


The code below will most definitely offer a more precise way of blocking, rather than calling Thread.Sleep(x); (although this method will block the thread, not put it to sleep). Below we are using the StopWatch class to measure how long we need to keep looping and block the calling thread.

using System.Diagnostics;  private static void NOP(double durationSeconds) {     var durationTicks = Math.Round(durationSeconds * Stopwatch.Frequency);     var sw = Stopwatch.StartNew();      while (sw.ElapsedTicks < durationTicks)     {      } } 

Example usage,

private static void Main() {     NOP(5); // Wait 5 seconds.      Console.WriteLine("Hello World!");      Console.ReadLine(); } 
like image 39
Sam Avatar answered Sep 18 '22 01:09

Sam