int main(){
int check1=0, check2=0;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
printf("g_Flag=%d\n", g_Flag);
pthread_mutex_lock(&mutex); //make sure that mutex is locked if cond_wait get the mutex first
check1=pthread_create(&thread[0], NULL, thread1, NULL);
if (check1!=0){
fprintf(stderr, "Thread1 create failed\n");
exit(1);
}
check2=pthread_create(&thread[1], NULL, thread2, NULL);
if (check2!=0){
fprintf(stderr, "Thread2 create failed\n");
exit(2);
}
sleep(1);//test if the cond signal works well
pthread_cond_wait(&cond, &mutex);
printf("EXIT MAN THREAD\n");
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
void* thread1(void* arg){
printf("This is thread1\n");
pthread_mutex_lock(&mutex);
if (g_Flag==2)
pthread_cond_signal(&cond);
g_Flag=1;
printf("g_Flag=%d\n", g_Flag);
pthread_mutex_unlock(&mutex);
pthread_join(thread[1], NULL);
printf("Exit thread1\n");
pthread_exit(NULL);
}
void* thread2(void* arg){
printf("This is thread2\n");
pthread_mutex_lock(&mutex);
if (g_Flag==1)
pthread_cond_signal(&cond);
g_Flag=2;
printf("g_Flag=%d\n", g_Flag);
pthread_mutex_unlock(&mutex);
printf("Exit thread2\n");
pthread_exit(NULL);
}
int main(){
int check1=0, check2=0;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
printf("g_Flag=%d\n", g_Flag);
pthread_mutex_lock(&mutex); //make sure that mutex is locked if cond_wait get the mutex first
check1=pthread_create(&thread[0], NULL, thread1, NULL);
if (check1!=0){
fprintf(stderr, "Thread1 create failed\n");
exit(1);
}
check2=pthread_create(&thread[1], NULL, thread2, NULL);
if (check2!=0){
fprintf(stderr, "Thread2 create failed\n");
exit(2);
}
sleep(1);//test if the cond signal works well
pthread_cond_wait(&cond, &mutex);
printf("EXIT MAN THREAD\n");
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
void* thread1(void* arg){
printf("This is thread1\n");
pthread_mutex_lock(&mutex);
if (g_Flag==2)
pthread_cond_signal(&cond);
g_Flag=1;
printf("g_Flag=%d\n", g_Flag);
pthread_mutex_unlock(&mutex);
pthread_join(thread[1], NULL);
printf("Exit thread1\n");
pthread_exit(NULL);
}
void* thread2(void* arg){
printf("This is thread2\n");
pthread_mutex_lock(&mutex);
if (g_Flag==1)
pthread_cond_signal(&cond);
g_Flag=2;
printf("g_Flag=%d\n", g_Flag);
pthread_mutex_unlock(&mutex);
printf("Exit thread2\n");
pthread_exit(NULL);
}
int main(){
int check1=0, check2=0;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
printf("g_Flag=%d\n", g_Flag);
pthread_mutex_lock(&mutex); //make sure that mutex is locked if cond_wait get the mutex first
check1=pthread_create(&thread[0], NULL, thread1, NULL);
if (check1!=0){
fprintf(stderr, "Thread1 create failed\n");
exit(1);
}
check2=pthread_create(&thread[1], NULL, thread2, NULL);
if (check2!=0){
fprintf(stderr, "Thread2 create failed\n");
exit(2);
}
sleep(1);//test if the cond signal works well
pthread_cond_wait(&cond, &mutex);
printf("EXIT MAN THREAD\n");
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
void* thread1(void* arg){
printf("This is thread1\n");
pthread_mutex_lock(&mutex);
if (g_Flag==2)
pthread_cond_signal(&cond);
g_Flag=1;
printf("g_Flag=%d\n", g_Flag);
pthread_mutex_unlock(&mutex);
pthread_join(thread[1], NULL);
printf("Exit thread1\n");
pthread_exit(NULL);
}
void* thread2(void* arg){
printf("This is thread2\n");
pthread_mutex_lock(&mutex);
if (g_Flag==1)
pthread_cond_signal(&cond);
g_Flag=2;
printf("g_Flag=%d\n", g_Flag);
pthread_mutex_unlock(&mutex);
printf("Exit thread2\n");
pthread_exit(NULL);
}
int main(){
int check1=0, check2=0;
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
printf("g_Flag=%d\n", g_Flag);
pthread_mutex_lock(&mutex); //make sure that mutex is locked if cond_wait get the mutex first
check1=pthread_create(&thread[0], NULL, thread1, NULL);
if (check1!=0){
fprintf(stderr, "Thread1 create failed\n");
exit(1);
}
check2=pthread_create(&thread[1], NULL, thread2, NULL);
if (check2!=0){
fprintf(stderr, "Thread2 create failed\n");
exit(2);
}
sleep(1);//test if the cond signal works well
pthread_cond_wait(&cond, &mutex);
printf("EXIT MAN THREAD\n");
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
void* thread1(void* arg){
printf("This is thread1\n");
pthread_mutex_lock(&mutex);
if (g_Flag==2)
pthread_cond_signal(&cond);
g_Flag=1;
printf("g_Flag=%d\n", g_Flag);
pthread_mutex_unlock(&mutex);
pthread_join(thread[1], NULL);
printf("Exit thread1\n");
pthread_exit(NULL);
}
void* thread2(void* arg){
printf("This is thread2\n");
pthread_mutex_lock(&mutex);
if (g_Flag==1)
pthread_cond_signal(&cond);
g_Flag=2;
printf("g_Flag=%d\n", g_Flag);
pthread_mutex_unlock(&mutex);
printf("Exit thread2\n");
pthread_exit(NULL);
}