这可能是一个非常基本的问题,但我正在使用下面的代码来运行一个简单的警报。它按我想要的方式工作,但我想知道是否有可能同时运行多个警报,每个警报在完成时触发不同的功能。有办法吗?
#include <signal.h>
#include <sys/time.h>
#include <stdio.h>
#include <time.h>
void alarm_handler(int signum){
printf("five seconds passed!!\n");
}
int main(){
signal(SIGALRM, alarm_handler);
alarm(5);
pause();
return 0;
}
最佳答案
没有。根据this source :
Alarm requests are not stacked; only one SIGALRM generation can be scheduled in this manner. If the SIGALRM signal has not yet been generated, the call shall result in rescheduling the time at which the SIGALRM signal is generated.
一种替代方法是创建一个 priority queue在其中放置任务,然后始终为当前时间与队列顶部任务之间的时差安排闹钟。
但一定要看this所以问题:你在信号处理程序中可以做的事情种类有限。
关于c - C中的多个警报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44122139/