简体   繁体   English

使用睡眠时的pthread调度问题

[英]pthread scheduling issue while using sleep

I spawn four detachable threads and in each thread i am giving 1 second sleep (sleep is my application requirement) because of that OS only schedule the 2 threads and other two threads are sitting ideal but if i remove sleep all threads are scheduled by OS but my application requirement is each thread should go to sleep mode once it complete its task 我产生了四个可分离的线程,并且在每个线程中我给出了1秒的睡眠(睡眠是我的应用程序要求),因为该操作系统仅安排2个线程,而其他两个线程处于理想状态,但是如果我删除睡眠,则所有线程均由OS安排,但是我的应用程序要求是每个线程在完成任务后都应进入睡眠模式

I already tried sleep, nanosleep and set the threads properties, priority and round robin scheduling in pthread but not success. 我已经尝试过睡眠,nanosleep和在pthread中设置线程属性,优先级和循环调度,但是没有成功。

Please suggest what else I can do to solve my application issue. 请提出其他解决我的应用程序问题的方法。

Now I am giving one second interval for each thread using below logic and removed the sleep from my code and issue is resolved (now all four threads are scheduled). 现在,我使用以下逻辑为每个线程分配一秒钟的间隔,并从代码中删除了睡眠,问题已解决(现在已计划了所有四个线程)。

(void) gettimeofday (&nowtime, NULL);
unsigned long long currentTime = (nowtime.tv_sec * 1000000) + nowtime.tv_usec;
unsigned long long FutureTime =  ((nowtime.tv_sec + interval) * 1000000) + nowtime.tv_usec;
while(FutureTime >= currentTime)
{
    (void) gettimeofday (&nowtime, NULL);
    currentTime = (nowtime.tv_sec * 1000000) + nowtime.tv_usec;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM