Linux, pthreads -- 发送停止条件

标签 linux pthreads posix

我正在 Linux 下使用 phtreads 和 C 编写应用程序。 main 函数启动了一堆线程(最多 20 个)。然后,根据某些标准,我需要立即停止所有线程。每个线程都在某个条件循环下运行,例如:

while (state) {....}

所以我需要为每个线程将状态更改为 false(我认为目前每个线程有一个状态就足够了,但也许将来我必须分别停止每个线程)

那么,最好的方法是什么?我可以使用一些 state 作为全局变量并为其使用互斥锁。每次我都必须锁定、读取、解锁它才能读取它的值。我认为这是一个非常耗时的过程。您对如何实现它有任何其他想法吗?

最佳答案

# man pthread_cancel

允许您向线程发送取消请求。

# man pthread_cleanup_push

允许您设置取消程序。

关于Linux, pthreads -- 发送停止条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11790181/

相关文章:

Linux ubuntu 如何停止 cron 状态邮件,如 "Cron <root@ip-myip>/usr/bin/php/var/www/myproject"

linux - 更改外部硬盘的安装位置时出错

linux - 具有私有(private)匿名映射的 ENOMEM 的 munmap() 失败

linux - 为主机系统无法访问的docker中的两台机器创建网络

linux - 反向排序文件中的第三个数字列

C Pthreads 问题,无法传递我想要的信息?

linux - 有没有一种简单的方法来 fork 文件描述符?

sockets - 哪些 Posix 套接字错误可以恢复?

c++ - 如何让线程等待对象的销毁

c - Pthreads、fread() 和 printf() : Getting random D4's in my string