zoukankan      html  css  js  c++  java
  • 线程取消

    int pthread_cancel(pthread_t th);

    该函数运行一个线程取消指定的另一个线程th

    函数成功,返回0,否则,返回非0;

    /***
    cancel.c
    ***/
    #include<stdio.h>
    #include<pthread.h>
    #include<errno.h>
    #include<string.h>
    #include<stdlib.h>
    
    void * func(void *arg)
    {
        while(1)
        {
            printf("fun run...
    ");
            sleep(1);
        }
        return NULL;
    }
    
    int main()
    {
        pthread_t t1;
        int err = pthread_create(&t1,NULL,func,NULL);
        if( 0 != err)
        {
            printf("thread_create failled : %s
    ",strerror(errno));
        }
        else
        {
            printf("thread_create success
    ");
        }
        sleep(5);
        pthread_cancel(t1);
        pthread_join(t1,NULL);
        return EXIT_SUCCESS;
    }

    函数运行结果:

    exbot@ubuntu:~/wangqinghe/thread/20190729$ ./cancel

    thread_create success

    fun run...

    fun run...

    fun run...

    fun run...

    fun run...

  • 相关阅读:
    Go语言的运算符
    Nginx基本安全优化
    在LNMP环境中部署一个blog服务程序
    PHP缓存加速器
    Go语言基础语法
    Go语言数据类型
    Go语言变量
    Go语言常量
    Go语言结构
    LNMP之PHP安装
  • 原文地址:https://www.cnblogs.com/wanghao-boke/p/11264648.html
Copyright © 2011-2022 走看看