【发布时间】:2014-07-05 14:42:22
【问题描述】:
我希望我的线程永远运行。在代码中的某处,如果收到“BYE”,它将终止整个进程。当我使用 while(TRUE) 或 for(;;) 时,我看到 CPU 使用率增加了 25%。睡眠功能也有限。有什么想法吗?
int _tmain(int argc, _TCHAR* argv[])
{
// Run the Standard Input/Output to reply to the super application
// (interface probably written in C#)
thread_standardIO_i = pthread_create(
&thread_standardIO_thrd,
NULL,
stdio_thread,
NULL);
while (1){
}
//sleep(9999999);
return 0;
}
【问题讨论】:
-
“有限”对您意味着什么?为什么睡不着?
-
如果您的输入/输出线程终止,首先就无法接收 BYE 命令,那么为什么不 (a) 直接加入该线程,或者更好的是 (b) 使 main线程输入/输出线程并在收到 BYE 时退出进程)。它实际上就像 invoking
stdio_thread一样简单,而不是旋转线程并尝试将“等待”范式串起来。 -
可能是 POSIX
pause()? -
while (!sleep(999));将循环直到进程收到一个信号,该信号导致睡眠返回剩余的睡眠时间。
-
mjan635,当您“看到 CPU 使用率增加 25%”时;那是你所期望的吗?如果不是,您对 CPU 利用率的目标影响是什么?
标签: c multithreading sleep main