Linux多线程编程的学习要点

2011年02月25日 19:42    发布者:hikesoso2010
#include   #include   #include   void *producter_f(void *arg);  void *consumer_f(void *arg);  int buffer_has_item = 0; /*设置缓存数量*/  pthread_mutex_t mutex; /*设置互斥*/  int running = 1;  int main (void)  {  pthread_t consumer_t; /*线程参数*/  pthread_t producter_t;  /*不知道这句为什么不给我变蓝色,初始化互斥*/  pthread_mutex_init(&mutex, NULL);  /*创建线程*/  pthread_create(&producter_t, NULL, (void *)producter_f, NULL);  pthread_create(&consumer_t, NULL, (void *)consumer_f, NULL);  usleep(1);  running = 0;  /*等待线程退出,一个线程不能够被多个线程等待*/  pthread_join(consumer_t, NULL);  pthread_join(producter_t, NULL);  /*销毁互斥*/  pthread_mutex_destroy(&mutex);  return 0;  }  void *producter_f(void *arg)  {  while (running)  {  pthread_mutex_lock(&mutex); /*加锁,进入互斥区*/  buffer_has_item++;  printf("product ,num:%d\n", buffer_has_item);  pthread_mutex_unlock(&mutex); /*解锁,离开互斥区*/  }  }  void *consumer_f(void *arg)  {  while (running)  {  pthread_mutex_lock(&mutex);  buffer_has_item--;  printf("consumer,num:%d\n",buffer_has_item);  pthread_mutex_unlock(&mutex);  }  }  /*  *生产者消费者问题的信号量控制,可以与上述程序进行对比,出处--同上  */  #include   #include   #include   void *producter_f(void *arg);  void *consumer_f(void *arg);  sem_t sem;  int running = 1;  int main (void)  {  pthread_t consumer_t;  pthread_t producter_t;  sem_init(&sem, 0, 16); /*信号量初始化*/  pthread_create(&producter_t, NULL, (void*)producter_f, NULL);  pthread_create(&consumer_t, NULL, (void *)consumer_f, NULL);  sleep(1);  running = 0;  pthread_join(consumer_t, NULL);  pthread_join(producter_t, NULL);  sem_destroy(&sem); /*销毁信号量*/  return 0;  }  void *producter_f(void *arg)  {  int semval = 0; /*信号量的初始值*/  while (running)  {  usleep(1);  sem_post(&sem); /*信号量+1*/  sem_getvalue(&sem, &semval);/*得到信号量的值*/  printf("pro,num:%d\n",semval);  }  }  void *consumer_f(void *arg)  {  int semval = 0;  while (running)  {  usleep(1);  sem_wait(&sem); /*信号量-1*/  sem_getvalue(&sem, &semval);  printf("con num:%d\n",semval);  }  }  /*  *条件变量来控制线程的同步,根据百度百科代码改编而成,不肯定没有错误  */  #include   #include   #include   void decrement_count(void);  void increment_count(void);  /**/  pthread_mutex_t count_lock;  /**/  pthread_cond_t count_nonzero;  unsigned count;  int main(void)  {  pthread_t decrement_t;  pthread_t increment_t;  /*初始化互斥*/  pthread_mutex_init(&count_lock, NULL);  pthread_create(&decrement_t, NULL, (void*)&decrement_count, NULL);  pthread_create(&increment_t, NULL, (void*)&decrement_count, NULL );  usleep(1);  pthread_join(decrement_t, NULL);  pthread_join(increment_t, NULL);  pthread_mutex_destroy(&count_lock);  return 0;  }  void decrement_count(void)  {  pthread_mutex_lock(&count_lock);  while (count == 0)  {/*使线程阻塞在一个条件变量上线程可以被函数pthread_cond_signal函数唤醒*/  pthread_cond_wait(&count_nonzero, &count_lock);  }  count = count - 1;  pthread_mutex_unlock(&count_lock);  }  void increment_count(void)  {  pthread_mutex_lock(&count_lock);  if (count == 0)  {/*它用来释放被阻塞在条件变量cond上的一个线程*/  pthread_cond_signal(&count_nonzero);  }  count = count + 1;  pthread_mutex_unlock(&count_lock);  }