线程同步中的死循环,该如何解决
线程同步中的死循环
当在main()函数中写完 同步的相关代码后,,会在后面加上:while(TRUE){};
这个我不懂了,,,{}中也没内容啊,,怎么会实现循环的,,我把while(TRUE){};注释掉之后,,程序就不运行
像下面这段,基本的生产者消费者问题:
int main()
{
pthread_t thread[2];
pthread_mutex_init(&mutex, NULL);
sem_init(&s, 0, 1);
sem_init(&delay, 0, 0);
init_queue(&msg);
if ( pthread_create(&thread[1], NULL, consumer, NULL) < 0)
{
printf("pthread_create\n");
}
if ( pthread_create(&thread[0], NULL, producer, NULL) < 0)
{
printf("pthread_create\n");
}
while(TRUE){};}
------解决方案--------------------
没内容就是空循环,为了让主进程不退出。注释掉之后主进程创建完线程后退出,创建的线程就终止了。
------解决方案--------------------
不用死循环 那么就用pthread_join吧
当在main()函数中写完 同步的相关代码后,,会在后面加上:while(TRUE){};
这个我不懂了,,,{}中也没内容啊,,怎么会实现循环的,,我把while(TRUE){};注释掉之后,,程序就不运行
像下面这段,基本的生产者消费者问题:
int main()
{
pthread_t thread[2];
pthread_mutex_init(&mutex, NULL);
sem_init(&s, 0, 1);
sem_init(&delay, 0, 0);
init_queue(&msg);
if ( pthread_create(&thread[1], NULL, consumer, NULL) < 0)
{
printf("pthread_create\n");
}
if ( pthread_create(&thread[0], NULL, producer, NULL) < 0)
{
printf("pthread_create\n");
}
while(TRUE){};}
------解决方案--------------------
没内容就是空循环,为了让主进程不退出。注释掉之后主进程创建完线程后退出,创建的线程就终止了。
------解决方案--------------------
不用死循环 那么就用pthread_join吧