Reputation: 261
I am looking for a way to pause a thread for accurate number of milliseconds in Java or C (I can use JNI to access the C method.
So far, I was using the following in java code.
LinkedBlockingQueue<String> SLEEPER = new LinkedBlockingQueue<String>();
SLEEPER.poll(msTime, TimeUnit.MILLISECONDS);
This was suggested on one of the threads in this forum and worked great on most of our windows7 machines.
But it is not giving me accrate results on a new set of hardware. So I decided to use JNI to access C. But even this does not pause for accurate amount of milliseconds on new hardwares (Dell and HP on Windows7).
JNIEXPORT void JNICALL Java_JniTimer_jniWait(JNIEnv *env, jobject obj , jint waitTime ) {
HANDLE hWaitEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (hWaitEvent)
{
WaitForSingleObject(hWaitEvent,waitTime);
CloseHandle (hWaitEvent);
}
}
Does anyone have a reliable option for accurate sleep on thread. Thanks.
Upvotes: 0
Views: 641
Reputation: 706
You can't do that on a non RT OS.
Not so much due to the resolution of timers, but because the per thread 'time slice' (sometimes referred to as quantum
), is usually set to a few ms (15 on my antique xp here), and is not something you can just play with.
Upvotes: 4