gpt4 book ai didi

c++ - 带定时器和信号的多线程程序

转载 作者:行者123 更新时间:2023-11-30 05:21:27 25 4
gpt4 key购买 nike

我是多线程编程的新手,所以这个问题可能看起来有点傻,下面是我的代码,我正在尝试拥有 2 个线程:

struct sigaction    S1,S2;
struct itimerval T1,T2;

void Function1(){
cout<<"Function1 "<<endl;
}
void Function2(){
cout<<"Function2 "<<endl;
}

void Thread_Function(){
int t=70000;
memset (&s2, 0, sizeof (s2));
s2.sa_handler = &Function2;

sigaction (SIGALRM, &s2, NULL);

sigemptyset(&s2.sa_mask);
s2.sa_flags = 0;

T2.it_value.tv_sec = 0;
T2.it_value.tv_usec =t;
T2.it_interval.tv_sec =0;
T2.it_interval.tv_usec =t;
setitimer (ITIMER_REAL, &T2, NULL);
}

int main(){

int t=50000;
memset (&s1, 0, sizeof (s1));
s1.sa_handler = &Function1;

sigaction (SIGALRM, &s1, NULL);

sigemptyset(&s1.sa_mask);
s1.sa_flags = 0;

T1.it_value.tv_sec = 0;
T1.it_value.tv_usec =t;
T1.it_interval.tv_sec =0;
T1.it_interval.tv_usec =t;
setitimer (ITIMER_REAL, &T1, NULL);

thread t1(Thread_Function);
}

问题是 Function1 没有执行,换句话说,我在输出中看不到 Function1

最佳答案

来自 the setitimer manual page

A process has only one of each of the three types of timers.

您正试图拥有两个相同类型的计时器,这是不可能的。

如果您想拥有多个计时器,则需要另一种解决方案。

关于c++ - 带定时器和信号的多线程程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40110035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com