#include<stdio.h>
#include<pthread.h>
pthread_cond_t cond;
pthread_mutex_t mutex;
void *r1(void *arg)
{
while(1)
{
pthread_cond_wait(&cond,&mutex);
printf("happy~!!!\n");
}
}
void *r2(void *arg)
{
while(1)
{
sleep(1);
pthread_cond_signal(&cond);
}
}
int main(void)
{
pthread_t t1,t2;
pthread_cond_init(&cond,NULL);
pthread_mutex_init(&mutex,NULL);
pthread_create(&t1,NULL,r1,NULL);
pthread_create(&t2,NULL,r2,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_cond_destroy(&cond);
pthread_mutex_destroy(&mutex);
}
多线程pthread函数的基本使用
最新推荐文章于 2024-08-02 05:00:00 发布