pthread_cond_wait(&cond, &mutex);////pthread_cond_wait用於等待某個特定的條件爲真
pthread_cond_signal(&cond);//用於通知阻塞的線程某個特定的條件爲真了
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<errno.h>
#include<unistd.h>
typedef void* (*fun)(void*);
int g_Flag=0;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void* thread1(void*);
void* thread2(void*);
void* thread3(void*);
void* thread4(void*);
/*
* when program is started, a single thread is created, called the initial thread or main thread.
* Additional threads are created by pthread_create.
* So we just need to create two thread in main().
*/
int main(int argc, char** argv)
{
printf("enter main\n");
pthread_t tid1, tid2, tid3, tid4;
int rc1=0, rc2=0, rc3=0, rc4=0;
int time=2;
rc4 = pthread_create(&tid4, NULL, thread4, NULL);
if(rc4 != 0)
printf("%s: %d\n",__func__, strerror(rc4));
printf("create thread4...\n");
rc3 = pthread_create(&tid3, NULL, thread3, NULL);
if(rc3 != 0)
printf("%s: %d\n",__func__, strerror(rc3));
printf("create thread3...\n");
rc2 = pthread_create(&tid2, NULL, thread2, NULL);
if(rc2 != 0)
printf("%s: %d\n",__func__, strerror(rc2));
printf("create thread2...\n");
rc1 = pthread_create(&tid1, NULL, thread1, &tid2);
if(rc1 != 0)
printf("%s: %d\n",__func__, strerror(rc1));
printf("create thread1...\n");
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);////pthread_cond_wait用於等待某個特定的條件爲真,pthread_cond_signal用於通知阻塞的線程某個特定的條件爲真了
pthread_mutex_unlock(&mutex);
//pthread_cond_timedwait(&cond, &mutex,&time);
printf("leave main\n");
exit(0);
}
/*
* thread1() will be execute by thread1, after pthread_create()
* it will set g_Flag = 1;
*/
void* thread1(void* arg)
{
printf("enter thread1\n");
printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
pthread_mutex_lock(&mutex);
if(g_Flag == 2)
{ pthread_cond_signal(&cond); //pthread_cond_wait用於等待某個特定的條件爲真,pthread_cond_signal用於通知阻塞的線程某個特定的條件爲真了
printf("pthread_cond_signal....\n");
}
g_Flag = 1;
printf("this is thread1, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
pthread_mutex_unlock(&mutex);
printf("wait pthread 2 exit...\n");
pthread_join(*(pthread_t*)arg, NULL);
printf("leave thread1\n");
pthread_cond_signal(&cond);
pthread_exit(0);
}
/*
* thread2() will be execute by thread2, after pthread_create()
* it will set g_Flag = 2;
*/
void* thread2(void* arg)
{
int i;
printf("enter thread2\n");
printf("this is thread2, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
pthread_mutex_lock(&mutex);
if(g_Flag == 1)
{
pthread_cond_signal(&cond);////pthread_cond_wait用於等待某個特定的條件爲真,pthread_cond_signal用於通知阻塞的線程某個特定的條件爲真了
printf("pthread_cond_signal....\n");
}
g_Flag = 2;
printf("this is thread2, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
pthread_mutex_unlock(&mutex);
for(i=0;i<5;i++)
{
printf("this is the thread2 i=%d\n",i);
sleep(1);
}
printf("leave thread2\n");
pthread_exit(0);
}
void* thread3(void* arg)
{
int i;
printf("enter thread3\n");
printf("this is thread3, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
pthread_mutex_lock(&mutex);
printf("thread3 wait for cond...\n");
pthread_cond_wait(&cond, &mutex);////pthread_cond_wait用於等待某個特定的條件爲真,pthread_cond_signal用於通知阻塞的線程某個特定的條件爲真了
printf("thread3 wait for cond success!\n");
printf("this is thread3, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
pthread_mutex_unlock(&mutex);
pthread_mutex_lock(&mutex);
printf("thread3 wait for cond....\n");
// pthread_cond_signal(&cond); //在同一個線程中,不能用pthread_cond_signal喚醒自身,必須由其他的線程喚醒?
pthread_cond_wait(&cond, &mutex);////pthread_cond_wait用於等待某個特定的條件爲真,pthread_cond_signal用於通知阻塞的線程某個特定的條件爲真了
printf("thread3 wait for cond success!!\n");
printf("this is thread3, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
pthread_exit(0);
}
void* thread4(void* arg)
{
int i;
printf("enter thread4\n");
printf("this is thread4, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
pthread_mutex_lock(&mutex);
printf("thread4 wait for cond...\n");
pthread_cond_wait(&cond, &mutex);////pthread_cond_wait用於等待某個特定的條件爲真,pthread_cond_signal用於通知阻塞的線程某個特定的條件爲真了
printf("thread4 wait for cond success!\n");
printf("this is thread4, g_Flag: %d, thread id is %u\n",g_Flag, (unsigned int)pthread_self());
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}