Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在编写一个多线程程序,我想在其中处理Ctrl-C来自用户的可能命令以终止执行。据我所知,不能保证能够取消每个工作线程的主线程会捕捉到信号。因此,是否有必要对工作线程的代码使用不同的信号处理程序,以便任何人在信号到达时都能捕捉到信号,或者是否有另一种方法可以仅在主线程的代码中使用信号处理程序?
您可以使用 pthread_sigmask() 阻止来自调用线程的信号。
而且,由于阻塞的信号被继承给新创建的线程,您可以在主线程中阻塞 SIGINT,然后启动其他线程,然后在主线程中解除阻塞,如果这样更可取的话。