The POSIX standard provides no mechanism by which a thread A can suspend the execution of another thread B, without cooperation from B. The only way to implement a suspend/restart mechanism is to have B check periodically some global variable for a suspend request and then suspend itself on a condition variable, which another thread can signal later to restart B.
翻译一下:POSIX 标准没有提供机制,让线程 A 在没有线程 B 的协作下暂停线程 B 的执行。
要想处理这种问题的话,信号处理或者使用条件变量吧。
有关 pthread_sigmask 的问题,pthread_sigmask 会继承当前的 mask, 你可以在 main 里定义好需要的 sigmask, 新建的线程会继承 mask。
使用信号会带来一些其他问题,比如外部给进程发信号,内核会地送到任意没有阻塞该信号的线程,可能会导致意料之外的线程睡眠 /唤醒。