如何从主线程唤醒睡眠线程? [英] How to wake up a sleeping thread from the main Thread?
问题描述
我有一个捕获程序,该程序除了捕获数据并将其写入文件外,还会打印一些统计信息.该功能可打印统计信息
I have a capture program which in addition do capturing data and writing it into a file also prints some statistics.The function that prints the statistics
static void report(void)
{
/*Print statistics*/
}
大约每秒钟都会使用一个警报来每秒调用一次,因此该程序就像
is called roughly every second using an ALARM that expires every second.So The program is like
void capture_program()
{
pthread_t report_thread
while()
{
if(pthread_create(&report_thread,NULL,report,NULL)){
fprintf(stderr,"Error creating reporting thread! \n");
}
/*
Capturing code
--------------
--------------
*/
if(doreport)
/*wakeup the sleeping thread.*/
}
}
void *report(void *param)
{
//access some register from hardware
//sleep for a second
}
计时器到期将设置doreport
标志.如果设置了此标志,则会调用report()
清除该标志.
The expiry of the timer sets the doreport
flag.If this flag is set report()
is called which clears the flag.
当计时器在主线程中关闭时,如何唤醒睡眠线程(运行report())?
How do I wake up the sleeping thread (that runs the report()) when the timer goes off in the main thread?
推荐答案
当 计时器在主线程中关闭?
How do I wake up the sleeping thread (that runs the report()) when the timer goes off in the main thread?
我认为条件变量是您要寻找的机制.在条件变量上具有report-thread块,并且每当您要唤醒报告线程时,主线程就会向条件变量发出信号(请参阅链接以获取更多详细说明).
I think a condition variable is the mechanism you are looking for. Have the report-thread block on the condition variable, and the main thread signal the condition variable whenever you want the report-thread to wake up (see the link for more detailed instructions).
这篇关于如何从主线程唤醒睡眠线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!