kakush
kakush

Reputation: 3484

CPP WINDOWS : is there a sleep function in microseconds?

I know there is for milliseconds (Sleep(milli))

but I couldn't find one for micro..

Upvotes: 10

Views: 28448

Answers (4)

Arno
Arno

Reputation: 5194

I just wrote a detailed comment about the sleep() function and spinning the performance counter. To avoid typing it here again, here is the link:

c++, usleep() is obsolete, workarounds for Windows/MingW?

Upvotes: 0

bames53
bames53

Reputation: 88155

The VS 11 dev preview includes the part of the standard library dealing with threads. So now you can say:

std::this_thread::sleep_for(std::chrono::microseconds(1));

Of course this doesn't mean the thread will wake up after exactly this amount of time, but it should be as close as the platform (and library implementation) allows for. As other comments have pointed out, Windows doesn't actually allow threads to sleep for durations this short.

Upvotes: 6

Lol4t0
Lol4t0

Reputation: 12547

You can use rdtsc instruction or QueryPerformanceCounter Windows API function to get high-resolution counters. You can calibrate them then with GetTickCount, or time functions for example.

Upvotes: 2

Cory Nelson
Cory Nelson

Reputation: 29981

Windows can't sleep for less than a millisecond. Time slices tend to be much higher than 1ms, so it isn't really possible even with a thread a highest priority.

If you don't care about burning CPU, you can spin until QueryPerformanceCounter has elapsed your time.

Upvotes: 2

Related Questions