pthread_kill 与pthread_cancel使用方法
来源:互联网 发布:两个数据库数据同步 编辑:程序博客网 时间:2024/05/23 21:36
pthread_kill 与pthread_cancel使用方法
#include <pthread.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;pthread_cond_t cond = PTHREAD_COND_INITIALIZER;void* thread0(void* arg){ pthread_mutex_lock(&mutex); printf("in thread 0 tag 1\n"); pthread_cond_wait(&cond, &mutex); printf("in thread 0 tag 2\n"); pthread_mutex_unlock(&mutex); printf("in thread 0 tag 3\n"); pthread_exit(NULL);}void* thread1(void* arg){ sleep(10); printf("in thread 1 tag 1\n"); pthread_mutex_lock(&mutex); printf("in thread 1 tag 2\n"); pthread_cond_broadcast(&cond); pthread_mutex_unlock(&mutex); printf("in thread 1 tag 3\n"); pthread_exit(NULL);}int main(){ pthread_t tid[2]; if (pthread_create(&tid[0], NULL, thread0, NULL) != 0) { exit(1); } if (pthread_create(&tid[1], NULL, thread1, NULL) != 0) { exit(1); } sleep(5); printf("in main thread tag 1\n"); pthread_cancel(tid[0]); //pthread_kill(tid[0],3); pthread_join(tid[0], NULL); printf("tid0\n"); pthread_join(tid[1], NULL); printf("tid1\n"); pthread_mutex_destroy(&mutex); pthread_cond_destroy(&cond); return 0;}使用pthread_cancel 结果如下:in thread 0 tag 1in main thread tag 1tid0in thread 1 tag 1pthread_cancel kill 掉线程,但是如果有类似拿锁的操作,则要等拿到锁才能kill掉。pthread_kill 结果如下:in thread 0 tag 1in main thread tag 1Quitpthread_kill 会对线程0发出signal_quit(3)的系统信号。 如果线程0不处理,则整个进程退出。
阅读全文
1 0
- pthread_kill 与pthread_cancel使用方法
- pthread_kill pthread_cancel
- pthread_kill和pthread_cancel
- pthread_kill和pthread_cancel
- pthread_kill和pthread_cancel
- pthread_kill和pthread_cancel
- pthread_kill和pthread_cancel
- pthread_cancel 和pthread_kill的区别
- pthread_kill()和pthread_cancel()的用法
- pthread_kill 使用方法
- pthread_cancel()与pthread_cancel
- pthread_cancel 使用方法1
- pthread_cancel 使用方法2
- 线程正常终止pthread_exit,pthread_join,pthread_kill,pthread_cancel,sigwait,sigaddset
- pthread_kill 与信号处理
- pthread_kill 与pthread_join
- pthread_kill
- linux 多线程与信号pthread_kill
- 网易机器学习笔试题
- MyBatis Generator generatorConfig.xml配置详解
- java基础第七天总结 继承
- 关于“微软停止信任沃通WoSign新证书”的说明
- 超有价值的xml简介
- pthread_kill 与pthread_cancel使用方法
- 微信小程序多张图片上传
- HDU 4549 M斐波那契数列 (费马小定理+矩阵快速幂)
- C++智能指针(三):weak_ptr--解决shared_ptr循环引用问题
- Java基础语法
- SSH整合开发之配置文件
- SPI通信协议
- lintcode--加油站
- dataTable导入combobox下拉框