使用 pthread_kill() 和 SIGSTOP 挂起和恢复线程
pthread_kill() 函数可以用于向指定线程发送信号。要使用 SIGSTOP 信号来挂起线程,可以使用以下代码:
#include <pthread.h>
#include <signal.h>
void suspend_thread(pthread_t thread) {
pthread_kill(thread, SIGSTOP);
}
void resume_thread(pthread_t thread) {
pthread_kill(thread, SIGCONT);
}
其中,suspend_thread() 函数用于挂起指定线程,resume_thread() 函数用于恢复线程。在调用 pthread_kill() 函数时,指定信号为 SIGSTOP 或 SIGCONT 即可实现线程的挂起和恢复。
原文地址: https://www.cveoy.top/t/topic/mXEw 著作权归作者所有。请勿转载和采集!