| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2262 人关注过本帖
标题:pthread中pthread_cleanup_pop()
取消只看楼主 加入收藏
花脸
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:9
帖 子:788
专家分:907
注 册:2017-1-4
结帖率:95.37%
收藏
 问题点数:0 回复次数:0 
pthread中pthread_cleanup_pop()
#include <iostream>
#include <pthread.h>
using namespace std;

pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
int a=0;//被互斥量和条件变量共同保护的数据

void cleanup_handle(void *)
{
   
    pthread_mutex_unlock(&mutex);   
}

void *route1(void *)//模拟出队操作的函数 即:a--
{
    int oldtype;
    pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);
    pthread_cleanup_push(cleanup_handle,NULL);
    pthread_mutex_lock(&mutex);
    while(a==0)
    {
        cout<<"Route1 running"<<endl;
        pthread_cond_wait(&cond,&mutex);
        cout<<"signal_1 is "<<a<<endl;
    }
    a--;
    cout<<"Route1 a is "<<a<<endl;
    pthread_mutex_unlock(&mutex);
    pthread_cleanup_pop(0);  //[Error] second operand to the conditional operator is of type 'void', but the third operand is neither a throw-expression nor of type 'void'
    pthread_exit(NULL);
   
}

void *route2(void *)//模拟入队的操作的函数 即:a++
{
    pthread_mutex_lock(&mutex);
    while(a==0)
    {   
        cout<<"Route2 running"<<endl;
        a++;
        pthread_cond_signal(&cond);
        cout<<"signal_2 is "<<a<<endl;
    }
    cout<<"Route2 a is "<<a<<endl;
    pthread_mutex_unlock(&mutex);
    pthread_exit(NULL);
}

int main()
{
    pthread_t pid1,pid2;
   
    int status=pthread_create(&pid1,NULL,route1,NULL);
    if(status)
    {
        cout<<"Pthread1 create error!"<<endl;
        exit(0);
    }
   
    status=pthread_create(&pid2,NULL,route2,NULL);
    if(status)
    {
        cout<<"Pthread2 create error!"<<endl;
        exit(0);
    }
   
    pthread_join(pid1,NULL);
    pthread_join(pid2,NULL);
    return 0;
}
搜索更多相关主题的帖子: int void NULL cout status 
2018-06-19 23:34
快速回复:pthread中pthread_cleanup_pop()
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017478 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved