|
在linux下面做了一个很小的多线程程序,很粗糙,用监视工具查看运行这个小程序时的CPU占用率100%, 请个位指点一下是什么导致的. 刚接触linux多线程,
/* thread.c */
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t mutex;
pthread_cond_t cond;
void * child1(void *arg)
{
int count = 0;
do
{
count ++;
pthread_cond_signal(&cond);
printf("cond signal = %d\n", count);
}while(1);
}
void *child2(void *arg)
{
int count;
while(1)
{
count ++;
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond,&mutex);
printf("thread 2 condition applied\n");
pthread_mutex_unlock(&mutex);
}
}
int main(void)
{
pthread_t tid1,tid2;
printf("hello, condition variable test\n");
pthread_mutex_init(&mutex,NULL);
pthread_cond_init(&cond,NULL);
pthread_create(&tid1,NULL,child1,NULL);
pthread_create(&tid2,NULL,child2,NULL);
sleep(1000);
pthread_exit(0);
}
/* gcc -o thread thread.c -lpthread */ |
|