日期:2014-05-16 浏览次数:20637 次
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
pthread_cond_t s_startupCond;
pthread_mutex_t s_startupMutex;
int child_thread_start=0;
void *child_thread()
{
pthread_mutex_lock(&s_startupMutex);
child_thread_start = 1;
printf("broadcast\n");
pthread_cond_broadcast(&s_startupCond);
pthread_mutex_unlock(&s_startupMutex);
}
int main()
{
pthread_t child_thread_value;
pthread_mutex_lock(&s_startupMutex);
pthread_attr_t attr;
pthread_attr_init (&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
printf("server_create_child_thread\n");
int err = pthread_create(&child_thread_value,&attr,child_thread,(void *)NULL);
while (child_thread_start == 0) {
printf("child_thread_start=0\n");
pthread_cond_wait(&s_startupCond, &s_startupMutex);
}
printf("pthread_cond_wait return and child_thread_start == 1\n");
child_thread_start=0;
pthread_mutex_unlock(&s_startupMutex);
return 0;
}
gcc -o test test.c -lpthread
server_create_child_thread
child_thread_start=0
broadcast
pthread_cond_wait return and child_thread_start == 1