zoukankan      html  css  js  c++  java
  • 线程pthread_cleanup_push的简单例程.

    http://www.cnblogs.com/hnrainll/archive/2011/04/20/2022149.html

    #include<stdlib.h>

    #include<stdio.h>
    #include<unistd.h>
    #include<pthread.h>
    void clean_fun1(void * arg)
    {
        printf("this is clean fun1 ");
    }
    void clean_fun2(void * arg)
    {
        printf("this is clean fun2 ");
    }
    void * thread_fun(void * arg)
    {
        pthread_cleanup_push(clean_fun1,NULL);
        pthread_cleanup_push(clean_fun2,NULL);
        sleep(100);
        //这里要注意,如果将sleep(100);换成while(1);的话,程序会一直暂停.push和pop要成对出现.
        //因为while(1);运行的太快,线程不接受cancel信号
        //while(1);
        pthread_cleanup_pop(0);
        pthread_cleanup_pop(0);
        return NULL;
    }
    int main()
    {
        pthread_t tid1;
        int err;
        err=pthread_create(&tid1,NULL,thread_fun,NULL);
        if(err!=0)
        {
            perror("pthread_create");
            exit(0);
        }
        sleep(3);
        //printf("test ");
        err=pthread_cancel(tid1);
        if(err!=0)
        {
            perror("cancel error:");
            exit(0);
        }
        err=pthread_join(tid1,NULL);
        if(err!=0)
        {
            perror("pthread_join  error:");
            exit(0);
        }
         
        return 0;
    }
  • 相关阅读:
    doT js模板入门
    @SuppressWarnings忽略警告
    SQL注入
    【转】Oracle 执行动态语句
    连接数据库——模拟ATM机查、存、取、开户功能
    Oracle 函数中动态执行语句
    PowerDesigner建表
    数据库表设计—水电费缴费系统
    GUID全局唯一标识符相关知识了解
    Oracle数据库建表+添加数据练习
  • 原文地址:https://www.cnblogs.com/feng9exe/p/6797132.html
Copyright © 2011-2022 走看看