#include <list>
#include <cstdio>
#include<cstdlib>
#include<unistd.h>
#include <exception>
#include <pthread.h>
#include<iostream>
#include<string>
#define num 3
using namespace std;
pthread_mutex_t lock;
volatile int XHL;
struct info{
int id;
string info;
};
void* fun(void* arg)
{
while(1)
{
XHL--;
if(XHL>0)
{
// pthread_mutex_lock(&lock);
auto i=(struct info*)arg;
sleep(3);
cout<<"thread_id:"<<i->id<<i->info<<endl;
// pthread_mutex_unlock(&lock);
XHL++;
// pthread_exit(nullptr);
}
}
}
int main(){
pthread_mutex_init(&lock,nullptr);
int a=2;
XHL=1;
pthread_t tid[num];
info t[num];
for(int i=0;i<num;i++)
{
cout<<"creat"<<endl;
t[i].id=i;
t[i].info="info";
int flag=pthread_create(&tid[i],nullptr,fun,(void*)&t[i]);
cout<<"pthread_self"<<pthread_self<<endl;
if(flag==-1)
cout<<"pthreat_creat error"<<endl;
}
for(int i=0;i<num;i++)
{
cout<<"main_thread"<<endl;
}
pthread_detach(tid[0]);
pthread_detach(tid[1]);
pthread_detach(tid[2]);
/* void* status;
for(int i=0;i<num;i++)
{
bool flag=pthread_join(tid[i],&status);
if(flag==1)
cout<<"join error"<<endl;
cout<<"exit status:"<<status<<endl;
}*/
pthread_exit(nullptr);
return -1;
}
|