调用系统函数pthread_cancel取消进程的其他线程
2011-10-10 23:04
621 查看
调用系统函数pthread_cancel取消进程的其他线程
先在主线程中创建一子线程,在子线程中一直输出运行了多少时间,在主进程监控,当发现子线程已经运行10秒的时候,取消子线程。
Demo代码如下:
注意编译的时候要加 -lpthread 选项,即unix的线程库。
运行结果如下图:
先在主线程中创建一子线程,在子线程中一直输出运行了多少时间,在主进程监控,当发现子线程已经运行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()函数(线程三)
- 系统调用-进程创建函数glibc部分2
- 线程取消(pthread_cancel)
- 子线程 run() 函数中,调用其他函数
- 【UNIX】进程系统调用的wait/waitpid()函数
- 线程取消(pthread_cancel)
- ptrace截获其他进程系统调用
- linux strace-跟踪进程的系统调用或是信号产生情况,lstrace-跟踪己丑年调用库函数情况,进程跟踪调试命令
- 线程取消(pthread_cancel)
- Linux常见文件系统操作命令、进程调用命令、进程控制C函数、时间函数
- 多线程,成员函数内创建线程,线程函数调用类内的其他成员函数
- Socket进程处理被中断的系统调用及Accept函数返回EINTR错误处理
- Linux--多线程之线程的取消pthread_cancel
- Linux进程间通讯所使用的系统调用函数
- 进程系统调用——fork函数深入理解
- Unix高级编程:库函数与系统调用函数区别、文件锁、进程基础
- 线程取消(pthread_cancel)
- 调用系统函数向进程发信号core
- 线程取消 (pthread_cancel)
- linux下pthread_cancel无法取消线程的原因