pthread_sigmask:异常行为
pthread_sigmask:unusual behavior
我正在使用Linux并尝试与信号处理相关的代码。按照我正在尝试的代码,但我无法理解此代码的行为。
/**Globally declared variable**/
time_t start, finish;
struct sigaction sact;
sigset_t new_set, old_set,test;
double diff;
/**Function to Catch Signal**/
void catcher( int sig )
{
cout<< "inside catcher() functionn"<<endl;
}
void Initialize_Signalhandler()
{
sigemptyset( &sact.sa_mask );
sact.sa_flags = 0;
sact.sa_handler = catcher;
sigaction( SIGALRM, &sact, NULL );
sigemptyset( &new_set );
sigaddset( &new_set, SIGALRM );
}
/**Function called by thread**/
void *threadmasked(void *parm)
{
/**To produce delay of 10sec**/
do {
time( &finish );
diff = difftime( finish, start );
} while (diff < 10);
cout<<"Thread Exit"<<endl;
}
int main( int argc, char *argv[] ) {
Initialize_Signalhandler();
pthread_t a;
pthread_create(&a, NULL, threadmasked, NULL);
pthread_sigmask( SIG_BLOCK, &new_set, &old_set);
time( &start );
cout<<"SIGALRM signals blocked at %sn"<< ctime(&start) <<endl;
alarm(2); //to raise SIGALM signal
/**To produce delay of 10sec**/
do {
time( &finish );
diff = difftime( finish, start );
} while (diff < 10);
return( 0 );
}
即使我正在使用" pthread_sigmask( SIG_BLOCK, &new_set, &old_set)"。 它不会阻挡信号。但是如果我删除"pthread_create(&a,空,线程屏蔽,空);" 它工作正常并阻止信号。我在这里观察到的另一件事是,如果我pthread_sigmask更改为 sigprocmask 行为保持不变。
线程从创建它们的线程继承信号掩码。
因此,当您的代码在调用pthread_create()
之后调用pthread_sigmask()
新创建的线程不会修改其信号掩码。
像这样更改代码,使事情按预期工作:
...
pthread_sigmask( SIG_BLOCK, &new_set, &old_set);
pthread_t a;
pthread_create(&a, NULL, threadmasked, NULL);
...
相关文章:
- 处理多个异常集合的C++方法
- 我在c++代码中生成了一个运行时#3异常
- 孤立代码块在结构中引发异常
- C++中的赋值发生,尽管右侧出现异常
- 从构造函数抛出异常时如何克服内存泄漏
- 异常属于C++中的线程还是进程
- 当类定义不可见时捕获异常
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- 为什么异常不退出程序?
- 为什么我应该在异常处理中使用std::cerr而不是std::cout
- 如何修复链表类实现的未处理异常0xDDDDDDDD
- 关于:C++中异常对象的范围:为什么我没有得到副本?
- 是什么导致了Unity 3D中的"错误线程异常"?
- 如何将strftime中的格式错误作为异常捕获
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 尝试使用智能指针时引发异常
- 函数如何通知用户它基于函数原型抛出异常?
- 是否值得降低我的代码的可读性,以便在出现内存不足错误时提供异常安全性?
- 当我使用 C++ 中的 C# dll 来使用 Selenium 时,存在异常处理问题
- Pytorch torch.cholesky忽略异常