#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 *arg)
{
pthread_mutex_unlock(&mutex);
}
void *route1(void *)//模擬出隊操作的函式 即:a--
{
int oldtype;
int *arg;
pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,&oldtype);
pthread_cleanup_push(cleanup_handle,(void*)arg);
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); //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;
}
pthread_cleanup_pop(0)這行出錯 錯誤資訊如上,求各位大佬指教。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/226668.html
標籤:專題技術討論區
下一篇:請問誰有運維的教學視頻嗎?
