日期:2014-05-16 浏览次数:20635 次
#include "errno.h" #include "stdio.h" #include "stdlib.h" #include "time.h" #include <pthread.h> #include "unistd.h" int a=0; pthread_mutex_t hMutex = PTHREAD_MUTEX_INITIALIZER; void * threadFunc1(void *arg) { while(1) { pthread_mutex_lock(&hMutex); (void)printf("aaa"); usleep(10); a++; printf("%d",a); pthread_mutex_unlock(&hMutex); pthread_yield(); } return 0; } void * threadFunc2(void *arg) { while(1) { pthread_mutex_lock(&hMutex); usleep(5); (void)printf("bbb\n"); a++; printf("%d",a); pthread_mutex_unlock(&hMutex); pthread_yield(); } return 0; } int main(void) { pthread_t tid1, tid2; printf("ccc"); pthread_setconcurrency(3); pthread_create(&tid1, NULL, threadFunc1, NULL); pthread_create(&tid2, NULL, threadFunc2, NULL); fflush(NULL); while(1) { usleep(1000); fflush(NULL); } return 0; }