调用系统函数pthread_cancel取消进程的其他线程
来源:互联网 发布:sql group by rollup 编辑:程序博客网 时间:2024/06/05 23:03
调用系统函数pthread_cancel取消进程的其他线程
先在主线程中创建一子线程,在子线程中一直输出运行了多少时间,在主进程监控,当发现子线程已经运行10秒的时候,取消子线程。
Demo代码如下:
注意编译的时候要加 -lpthread 选项,即unix的线程库。
#include <iostream>#include <pthread.h>using namespace std;void * thread_fun(void * arg) {cout << "child thread ID : " << pthread_self() << endl;int cnt = 0;while (true) {sleep(1);cout << "child thread run " << ++cnt << "second" << endl;}}int main(){int err;pthread_t tid1;err = pthread_create(&tid1, NULL, thread_fun, NULL);if (err != 0) {cout << "can't create thread" << endl;}int cnt = 0;while (true) {sleep(1);cnt++;if (cnt == 10) {cout << "main thread cancel the child thread."<< endl;cout << "main thread ID : " << pthread_self() << endl;cout << "child thread ID : " << tid1 << endl;pthread_cancel(tid1);break;}}return 0;}
运行结果如下图:
- 调用系统函数pthread_cancel取消进程的其他线程
- 线程的取消pthread_cancel()函数(线程三)
- pthread_cancel 线程取消以及锁的释放
- pthread_cancel 线程取消以及锁的释放
- Linux--多线程之线程的取消pthread_cancel
- pthread_cancel,取消一个线程
- 线程取消(pthread_cancel)
- 线程取消(pthread_cancel)
- 线程取消(pthread_cancel)
- 线程取消(pthread_cancel)
- 线程取消(pthread_cancel)
- 线程取消(pthread_cancel)
- 线程取消(pthread_cancel)
- 线程取消(pthread_cancel)
- 线程取消(pthread_cancel)
- 线程取消(pthread_cancel)
- 线程取消(pthread_cancel)
- 线程之线程取消(pthread_cancel)
- VMware Infrastructure 3常见管理问题解答
- POJ 2516 Minimum Cost (最小费用最大流,KM解法)
- Install VirtualBox 4.1.4 on Fedora 15/14, CentOS/Red Hat (RHEL) 5.6/6
- 数据结构_图_求无向图的关节点
- JDK的动态代理深入解析(Proxy,InvocationHandler)
- 调用系统函数pthread_cancel取消进程的其他线程
- curl/fsockopen/file_get_contents 三个函数 都可以实现采集模拟发言,三者有什么区别,或者讲究么?
- asp.net在后台获取html控件值
- 图书馆管理系统_DOS版
- shell的循环
- IEC 61850-9-1和IEC 61850-9-2采样值传输通信标准的对比
- NTP网络校时协议
- Linux中/proc目录下文件详解(一)
- #pragma warning()支持