线程互斥用互斥锁,线程的同步则用到条件变量。

条件变量是用来描述线程间同步的。

下面以生产者、消费者为例

生产者和消费者之间存在同步与互斥的关系。生产者之间、消费者之间存在互斥的关系


  #include<stdio.h>
  #include<pthread.h>
  #include<stdlib.h>
   
  static int i = 1;
  pthread_mutex_t lock;  //用来表述生产者和消费者之间的互斥关系的
  pthread_mutex_t lock1; //表述生产者和生产者之间的互斥关系
  pthread_mutex_t  lock2; //表述
  pthread_cond_t cond;   //表述生产者和消费者之间同步关系
  typedef struct node   
  {
     int _data;
     struct node* _next;
  }node;
  
  struct node *head;   //定义一个带头结点的链表
  node* buy_node(node* head,int data) 
  {
      node* list = malloc(sizeof(node*));
      list->_data = data;
      list->_next = NULL;
      return list;
  }
  void init(node** head)  //链表的初始化
  {
      *head = buy_node(*head,0);
  }
  void push(node* head,int data)  //头插数据
  {
      node* tmp = buy_node(head,data);
      tmp->_next = head->_next;
      head->_next = tmp;
  }
  int pop(node* head,int* arr)  //删除数据,并且写入arr里
  {
      if(head->_next == NULL)
      {
          *arr = -1;
          return -1;
      }
          //头删   数据后入先出
      //node* tmp = head->_next;
      //head->_next = tmp->_next;
      //*arr = tmp->_data;
          //尾删  相当于数据先入先出
      node* tmp = head;
      node* prev = NULL;
      while(tmp->_next)
      {
          prev = tmp;
          tmp = tmp->_next;
      }
      prev->_next = NULL;
      *arr = tmp->_data;
      free(tmp);
      tmp = NULL;
      return 0;
  }
  
  void *product(void *arg)  //生产者进程
  {
      //int i = 1;
      while(1)
      {
              pthread_mutex_lock(&lock1); //生产者、生产者之间的互斥锁
          pthread_mutex_lock(&lock); //生产者、消费者之间互斥锁
          printf("product %d:%d\n",(int)arg,i);
          push(head,i++);
          pthread_mutex_unlock(&lock);
          pthread_cond_signal(&cond);   //线程唤醒
          pthread_mutex_unlock(&lock1); 
          sleep(1);
      }
  }
  void *consumer(void *arg)
  {
      int buf;
      while(1)
      {
              pthread_mutex_lock(&lock2); //消费者之间的互斥锁
          pthread_mutex_lock(&lock); //生产者、消费者之间互斥锁
          while(pop(head,&buf)==-1)  
          {
              printf("consumer wait... product done...\n");
              pthread_cond_wait(&cond,&lock); //线程等待
          }
          pthread_mutex_unlock(&lock);
          printf("consumer %d:%d\n",(int)arg,buf);
          pthread_mutex_unlock(&lock2);
          sleep(1);
      }
  }
  int main()    
  {
      init(&head);
      pthread_mutex_init(&lock,NULL);
      pthread_mutex_init(&lock1,NULL);
      pthread_mutex_init(&lock2,NULL);
      pthread_cond_init(&cond,NULL);
      pthread_t tid1,tid2,tid3,tid4;
      pthread_create(&tid1,NULL,product,(void*)1);
      pthread_create(&tid2,NULL,consumer,(void*)1);
      pthread_create(&tid3,NULL,product,(void*)2);
      pthread_create(&tid4,NULL,consumer,(void*)2);
 
      pthread_join(tid1,NULL);
      pthread_join(tid2,NULL);
      pthread_join(tid3,NULL);
      pthread_join(tid4,NULL);
      pthread_mutex_destroy(&lock);
      pthread_mutex_destroy(&lock1);
      pthread_mutex_destroy(&lock2);
      pthread_cond_destroy(&cond);
      return 0;
   }      

//只有一个生产者一个消费者时的结果
[fbl@localhost cond]$ ./my_cond
consumer wait... product done...
product:1
consumer:1
consumer wait... product done...
product:2
consumer:2
consumer wait... product done...  

//两个生产者 两个消费者时的结果
[fbl@localhost cond]$ ./my_cond 
consumer wait... product done...
product 2:1
consumer 2:1
consumer wait... product done...
product 1:2
consumer 1:2
product 2:3
consumer 2:3
product 1:4
consumer 1:4
product 2:5
consumer 2:5
product 1:6
consumer 1:6
product 2:7
consumer 2:7
product 1:8
consumer 1:8
product 2:9
consumer 2:9
product 1:10
consumer 1:10


生产者之间、消费者之间各自需要一把互斥锁来管理。