linux使用线程锁访问互斥资源:
1、线程锁的创建
pthread_mutex_t g_Mutex;
2、完整代码如下
1 #include <stdio.h> 2 #include <string.h> 3 #include <pthread.h> 4 #include <stdlib.h> 5 #include <unistd.h> 6 #include <errno.h> 7 #include <signal.h> 8 9 #define READ_TIME 20000 10 #define WRITE_TIME 30000 11 12 pthread_mutex_t g_Mutex; 13 int g_iX = 0; 14 int g_rwok = 0; 15 16 bool bExit = false; 17 18 void sig(int signal) 19 { 20 bExit = true; 21 } 22 23 /* writer pthread, write per 30000 us */ 24 void * writer(void * arg) 25 { 26 while(1) 27 { 28 if(true == bExit) 29 { 30 g_rwok++; 31 break; 32 } 33 if(EBUSY != pthread_mutex_trylock(&g_Mutex)) 34 { 35 printf("