.取消线程

/*0.取消线程
  int pthread_cancel(pthread_t thread);
设置取消点
  void pthread_testcancel(void);
测试是否接收到取消请求,如果有,结束线程。
例子:*/
#include <stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <sched.h>
int a = 0;
void *thread1(void *arg)
{
         pthread_testcancel(); 这个函数非常重要要和 pthread_cancel(t1),结合起来使用;
         a = 10;
}
/*如果改为:
 int a = 0;
void *thread1(void *arg)
{
         a = 10;
       pthread_testcancel();
 }
运行结果:a值被修改了
main start
main end, a=10*/
int main(int argc, char *argv[])
{
        pthread_t  t1, t2, t3;
        int ret, i;
        printf("main start\n");
        ret = pthread_create(&t1, NULL, thread1, NULL);
        pthread_cancel(t1);
        pthread_join(t1, NULL);
        sleep(3);
        printf("main end, a=%d\n", a);
      return 0;
}
/*运行结果:在取消点处程序结束,a值未该。
main start
main end, a=0*/
时间: 2024-07-30 05:51:21

.取消线程的相关文章

取消线程,是否会释放线程的所有资源?

#include <stdlib.h> #include <pthread.h> #include <stdio.h> #include <sched.h> //取消线程,是否会释放线程的所有资源?例子: void *thread1(void *arg) { printf("start thread (%u)\n", (unsigned)pthread_self()); } int main(int argc, char *argv[])

linux下pthread_cancel无法取消线程的原因

一个线程可以调用pthread_cancel终止同一进程中的另一个线程,但是值得强调的是:同一进程的线程间,pthread_cancel向另一线程发终止信号.系统并不会马上关闭被取消线程,只有在被取消线程下次系统调用时,才会真正结束线程.或调用pthread_testcancel,让内核去检测是否需要取消当前线程.被取消的线程,退出值,定义在Linux的pthread库中常数PTHREAD_CANCELED的值是-1. #include <pthread.h> int pthread_canc

linux下pthread_cancel无法取消线程的原因【转】

转自:http://blog.csdn.net/huangshanchun/article/details/47420961 版权声明:欢迎转载,如有不足之处,恳请斧正. 一个线程可以调用pthread_cancel终止同一进程中的另一个线程,但是值得强调的是:同一进程的线程间,pthread_cancel向另一线程发终止信号.系统并不会马上关闭被取消线程,只有在被取消线程下次系统调用时,才会真正结束线程.或调用pthread_testcancel,让内核去检测是否需要取消当前线程.被取消的线程

线程学习第二课--脱离线程,调度线程,取消线程,多线程

例如主线程继续为用户提供服务的同时创建第二个线程这个线程的作用是将用户正在编辑的数据进行备份存储那么备份结束之后第二个线程就可以字节终止没必要再回到主线程中区 称这样的线程为脱离线程,可以通过修改属性或者调用pthread_detach的方法来创建这里我们从属性的角度研究脱离线程 1 #include <pthread.h> 2 int pthread_atte_init(pthread_attr_t * attr); 函数的作用是初始化一个线程属性对象 对应的回收函数是pthread_att

C# Thread.Abort方法与ThreadAbortException异常(取消线程与异常处理)

1.Abort当前线程,后续程序不会执行 class Program { public static Thread thread1; static void Main(string[] args) { thread1 = new Thread(Method1); thread1.Start(); Console.ReadKey(); } public static void Method1() { try { for (int i = 0; i < 10; i++) { Console.Writ

POSIX多线程--线程取消

1.三种取消状态Off                   禁用取消Deferred           推迟取消:在下一个取消点执行取消Asynchronous   异步取消:可以随时执行取消 int pthread_cancel(pthread_t thread) 2.推迟取消:在下一个取消点执行取消 Pthreads系统上的某些函数会被作为取消点,如pthread_testcancel,sleep,pthread_cond_wait等.线程调用pthread_cancel函数后,被取消线程

UNIX环境编程学习笔记(28)——多线程编程(三):线程的取消

lienhua342014-11-24 1 取消线程 pthread 提供了pthread_cancel 函数用于请求取消同一进程中的其他线程. #include <pthread.h> int pthread_cancel(pthread_t tid); 返回值:若成功则返回0,否则返回错误编码 pthread_cancel 调用并不会立即终止目标线程,而只是向目标线程发出取消请求.调用线程不等待目标线程终止,在默认情况下,目标线程在取消请求发出以后还是继续运行的,直到目标线程到达某个取消点

(转载)Linux 线程取消(Pthread_cancel)

线程取消(pthread_cancel) 基本概念pthread_cancel调用并不等待线程终止,它只提出请求.线程在取消请求(pthread_cancel)发出后会继续运行,直到到达某个取消点(CancellationPoint).取消点是线程检查是否被取消并按照请求进行动作的一个位置. 与线程取消相关的pthread函数int pthread_cancel(pthread_t thread)发送终止信号给thread线程,如果成功则返回0,否则为非0值.发送成功并不意味着thread会终止

pthread_setcanceltype 线程取消

取消线程: (1)一个线程可以调用pthread_cancel来取消另一个线程.    (2)被取消的线程需要被join来释放资源.    (3)被取消的线程的返回值为PTHREAD_CANCELED 有关线程的取消,一个线程可以为如下三个状态:          (1)可异步取消:一个线程可以在任何时刻被取消.          (2)可同步取消:取消的请求被放在队列中,直到线程到达某个点,才被取消.         (3)不可取消:取消的请求被忽略.            默认状态下,线程是