Paste
#82741: Untitled C paste by 113.108.133.42
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
typedef struct _test
{
int a;
pthread_mutex_t mutex;
pthread_cond_t cond;
} test;
test mytest= {0,PTHREAD_MUTEX_INITIALIZER,PTHREAD_COND_INITIALIZER};
void *proc(void *ptr)
{
pthread_detach(pthread_self());
pthread_mutex_lock(&mytest.mutex);
while(mytest.a!=5)
{
pthread_cond_wait(&mytest.cond,&mytest.mutex);
}
printf("%d\n",mytest.a);
pthread_mutex_unlock(&mytest.mutex);
return NULL;
}
int main()
{
pthread_t id;
pthread_create(&id,NULL,proc,NULL);
sleep(1);
while(mytest.a!=10)
{
pthread_mutex_lock(&mytest.mutex);
mytest.a++;
pthread_cond_signal(&mytest.cond);
pthread_mutex_unlock(&mytest.mutex);
sleep(1);
}
sleep(30);
return 0;
}