开发者

sleep command in threaded application

开发者 https://www.devze.com 2023-02-25 19:09 出处:网络
The Sleep(ms) command in windows is causing threads to release their timeslices. Is there an equivalent Sleep(ms) command that halts the thread but does not release the timesli开发者_JAVA百科ce?You do

The Sleep(ms) command in windows is causing threads to release their timeslices. Is there an equivalent Sleep(ms) command that halts the thread but does not release the timesli开发者_JAVA百科ce?


You don't want your thread to sleep (aka suspend), you want to stall it. Do that with a simple loop:

#include <time.h>

void stall(unsigned ms){
    clock_t goal = clock()+ms;
    while(goal>clock());
}
// or maybe higher resolution with some performance profiling functions...


Strange.

A way to do that is to make your thread execute a loop till the end of the periode needed.

A better solution may be to give the thread handling the device a high priority. But anyway, Windows is not very suitable for real-time systems...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号