线程的问题,可能需要看代码??(new)
自由的风 2004-04-02 10:48:00 pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t mycond;
void *proc(void *arg)
{
while(1)
{
pthread_mutex_lock(&mymutex); //lock
printf("begin ");
pthread_cond_wait(&mycond, &mymutex);
printf("test\n");
pthread_mutex_unlock(&mymutex);
}
}
int main(int argc, char *argv[])
{
pthread_t mythread;
int i;
if ( pthread_create( &mythread, NULL, proc, NULL) )
{
printf("error creating thread.");
abort();
}
pthread_cond_init(&mycond,NULL); //initialize
sleep(1);
for ( i=0; i<20; i++)
{
pthread_cond_broadcast(&mycond);
sleep(1);
}
pthread_cond_destroy(&mycond);
return EXIT_SUCCESS;
}
问题来了:编译通过了,我期望的结果是:先输出"begin" 一秒后接着输出"test",连续20次。但是程序一进入,就阻塞了,没有任何输出,能帮我看看吗?