除了上次讲的互斥锁,使用信号量也就是操作系统中所提到的PV原语,也能达到互斥和同步的效果,这就是下面要说的信号量线程控制。
 
         PV原语是对整数计数器信号量sem的操作,一次P操作可使sem减一,而一次V操作可是sem加一。进程(或线程)根据信号量的值来判断是否对公共资源具有访问权限。当信号量的值大于零或等于零的时候,该进程(或线程)具有对公共资源访问的权限,否则,当信号量的值小于时,该进程(或线程)就会被阻塞,直到信号量的值大于或等于一。
         
         在LINUX中,实现了POSIX的无名信号量,主要用于线程间的互斥同步,下面将简单介绍一些函数接口:
 
sem_init
功能:         用于创建一个信号量,并初始化信号量的值。
函数原型:     int sem_init (sem_t* sem, int pshared, unsigned int value);
 
int sem_wait       (sem_t* sem);
int sem_trywait   (sem_t* sem);
int sem_post       (sem_t* sem);
int sem_getvalue (sem_t* sem);
int sem_destroy   (sem_t* sem);
 
功能:
        sem_wait和sem_trywait相当于P操作,它们都能将信号量的值减一,两者的区别在于若信号量的值小于零时,sem_wait将会阻塞进程,而sem_trywait则会立即返回。
        sem_post相当于V操作,它将信号量的值加一,同时发出唤醒的信号给等待的进程(或线程)。
        sem_getvalue 得到信号量的值。
        sem_destroy 摧毁信号量。
 
程序实例如下:
 
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <errno.h>
#include <semaphore.h>
 
#define return_if_fail(p) \
         if(!p) { printf("[%s]:func error!", __func__); return; }
 
typedef struct _PrivInfo {
         sem_t sem;
         int lock_var;
         time_t end_time;
}PrivInfo;
 
void  info_init(PrivInfo *thiz);
void *pthread_function1(void *paramthiz);
void *pthread_function2(void *paramthiz);

int main (int argc, char** argv) {
         pthread_t pt_1 = 0;
         pthread_t pt_2 = 0;
         int ret = 0;
         PrivInfo *thiz = NULL;
         thiz = (PrivInfo*)malloc(sizeof(PrivInfo));
         if(NULL == thiz) {
          return -1;
         }
         info_init(thiz);
         ret = pthread_create(&pt_1, NULL, pthread_function1, (void*)thiz);
         if(0 != ret) {
                  perror("pthread1 creation failed!"); 
         }
 
         ret = pthread_create(&pt_2, NULL, pthread_function2,(void*)thiz);
         if(0 != ret) {
                  perror("pthread2 creation failed!"); 
         }
         pthread_join(pt_1, NULL);
         pthread_join(pt_2, NULL);
         sem_destroy(&thiz->sem);
 
         free(thiz); 
         thiz = NULL;
 
         return 0;
}
 
void info_init(PrivInfo *thiz) {
         return_if_fail(&thiz != NULL);
         thiz->lock_var = 0;
         thiz->end_time = time(NULL) + 10;
         sem_init(&thiz->sem, 0, 1);
 
         return;
}
 
void *pthread_function1(void *paramthiz) { 
         int i = 0;
         PrivInfo *thiz = (PrivInfo *)paramthiz;
         while(time(NULL) < thiz->end_time) {
                  sem_wait(&thiz->sem);
                  printf("thread1 get the lock.\n");
  
                  for(; i<2; i++) {
                           thiz->lock_var ++;
                           sleep(3);
                  }
  
                  sem_post(&thiz->sem);
                  printf("thread1 unlock.\n");
         }
  
         sleep(1);
         pthread_exit(NULL);
}
 
void *pthread_function2(void *paramthiz) { 
         PrivInfo *thiz = (PrivInfo *)paramthiz;
         while(time(NULL) < thiz->end_time) {
                  sem_wait(&thiz->sem);
                  printf("thread2 get the lock. The lock_var = %d.\n",
                                 thiz->lock_var);
  
                  sem_post(&thiz->sem);
                  printf("thread2 unlock.\n");
 
                  sleep(3);
         }
  
         pthread_exit(NULL);
}