新丰县建设局网站,申请专利的网站,培训课程设计方案,建设通网站会员共享密码多线程中#xff0c;信号在哪个线程中处理是不确定的#xff0c;可能被任意一个线程处理
下边的代码可以验证该结论#xff0c;多次Ctrlc#xff0c;会被不同的线程捕获此信号#xff0c;并处理#xff0c;最终每个线程死锁#xff0c;阻塞在等待锁的状态
#include 信号在哪个线程中处理是不确定的可能被任意一个线程处理
下边的代码可以验证该结论多次Ctrlc会被不同的线程捕获此信号并处理最终每个线程死锁阻塞在等待锁的状态
#include stdio.h
#include stdlib.h
#include signal.h
#include pthread.h
#include unistd.h
pthread_mutex_t lock;
// 信号处理函数
void sigint_handler(int signum) {printf(Received SIGINT signal, %ld\n,pthread_self());printf(lock line %d\n,__LINE__);pthread_mutex_lock(lock);//usleep(100);pthread_mutex_unlock(lock);printf(unlock line %d\n,__LINE__);
}// 线程函数
void* thread_func(void* arg) {while (1) { printf(thread process, %ld\n,pthread_self());usleep(100000);}return NULL;
}int main() {signal(SIGINT, sigint_handler); // 注册SIGINT信号的处理函数printf(main process, %ld\n,pthread_self());pthread_t thread1, thread2;pthread_create(thread1, NULL, thread_func, NULL); // 创建一个新线程pthread_create(thread2, NULL, thread_func, NULL); // 创建一个新线程while(1){printf(lock line %d\n,__LINE__); pthread_mutex_lock(lock);usleep(100000);pthread_mutex_unlock(lock);printf(unlock line %d\n,__LINE__);}pthread_join(thread1, NULL); // 等待新线程退出pthread_join(thread2, NULL); // 等待新线程退出return 0;
} Linux多线程信号处理浅谈_linux信号处理函数在哪个线程_hdxbw-wq的博客-CSDN博客