Linux 线程取消
2012-11-29 11:14
211 查看
#include <iostream>
#include <pthread.h>
using namespace std;
void *thread_function(void* arg);
int main(void)
{
int res;
pthread_t
threadID;
void *thread_result;
res = pthread_create(&threadID, NULL, thread_function, NULL);
if (res != 0)
{
perror("Thread Create Failed!");
exit(EXIT_FAILURE);
}
sleep(3);
printf("Cancleing Thread...\n");
//线程终止,取消线程
res = pthread_cancel(threadID);
if (res != 0)
{
perror("Thread Cancel Failed!");
exit(EXIT_FAILURE);
}
printf("Waiting For Thread To Finished...\n");
res = pthread_join(threadID, &thread_result);
if (res != 0)
{
perror("Thread Join Failed!");
exit(EXIT_FAILURE);
}
printf("Thread Canceled!");
return(EXIT_SUCCESS);
}
void *thread_function(void* arg)
{
int i, res;
//取消状态
PTHREAD_CANCEL_ENABLE 允许线程接收取消请求
res = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
if (res != 0)
{
perror("Thread Setcancelstate Failed!");
exit(EXIT_FAILURE);
}
//设置取消类型
PTHREAD_CANCEL_DEFERRED
//接收到取消请求后,一直等待直到线程执行了下述函数之一后才采取行动
res = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
if (res != 0)
{
perror("Thread Setcanceltype Failed!");
exit(EXIT_FAILURE);
}
printf("thread_function is running......\n");
for(i=0;i<10;i++)
{
printf("Thread is still running (%d) \n", i);
sleep(1);
}
pthread_exit(0);
}
run:
./a.out
thread_function is running......
Thread is still running (0)
Thread is still running (1)
Thread is still running (2)
Cancleing Thread...
Waiting For Thread To Finished...
Thread Canceled!
#include <pthread.h>
using namespace std;
void *thread_function(void* arg);
int main(void)
{
int res;
pthread_t
threadID;
void *thread_result;
res = pthread_create(&threadID, NULL, thread_function, NULL);
if (res != 0)
{
perror("Thread Create Failed!");
exit(EXIT_FAILURE);
}
sleep(3);
printf("Cancleing Thread...\n");
//线程终止,取消线程
res = pthread_cancel(threadID);
if (res != 0)
{
perror("Thread Cancel Failed!");
exit(EXIT_FAILURE);
}
printf("Waiting For Thread To Finished...\n");
res = pthread_join(threadID, &thread_result);
if (res != 0)
{
perror("Thread Join Failed!");
exit(EXIT_FAILURE);
}
printf("Thread Canceled!");
return(EXIT_SUCCESS);
}
void *thread_function(void* arg)
{
int i, res;
//取消状态
PTHREAD_CANCEL_ENABLE 允许线程接收取消请求
res = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
if (res != 0)
{
perror("Thread Setcancelstate Failed!");
exit(EXIT_FAILURE);
}
//设置取消类型
PTHREAD_CANCEL_DEFERRED
//接收到取消请求后,一直等待直到线程执行了下述函数之一后才采取行动
res = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
if (res != 0)
{
perror("Thread Setcanceltype Failed!");
exit(EXIT_FAILURE);
}
printf("thread_function is running......\n");
for(i=0;i<10;i++)
{
printf("Thread is still running (%d) \n", i);
sleep(1);
}
pthread_exit(0);
}
run:
./a.out
thread_function is running......
Thread is still running (0)
Thread is still running (1)
Thread is still running (2)
Cancleing Thread...
Waiting For Thread To Finished...
Thread Canceled!
相关文章推荐
- Linux学习笔记23——取消线程
- Linux_ pthread 线程的取消
- linux高编线程-------线程的取消
- linux小实验(4)---线程取消
- linux 线程取消以及条件变量锁的释放
- linux下pthread_cancel无法取消线程的原因【转】
- Linux 进程与线程二(等待--分离--取消线程)
- POSIX 线程取消点的 Linux 实现
- linux程序设计——取消一个线程(第十二章)
- linux线程的取消(终止)方法
- Linux--多线程之线程的取消pthread_cancel
- POSIX 线程取消点的 Linux 实现
- Linux编程——终止线程的正常方式及取消点
- POSIX 线程取消点的 Linux 实现
- POSIX 线程取消点的 Linux 实现
- linux下pthread_cancel无法取消线程的原因
- linux线程创建、取消、属性设置
- linux下pthread_cancel无法取消线程的原因
- POSIX 线程取消点的 Linux 实现(zz)
- linux程序设计——取消一个线程(第十二章)