新闻  |   论坛  |   博客  |   在线研讨会
Linux多线程编程的学习要点
hikesoso2010 | 2011-02-25 19:24:57    阅读:1099   发布文章

推荐初学者一个编程技术的学习网站,96堆栈 软件编程网,http://www.96dz.com,里面有C++视频教程、C#视频教程、Java视频教程下载,还有C\C++、Java、C# .NET等编程技术文摘,包括目前主流的Linux编程与Web编程等学习资料视频教程下载。 

 /*

  *生产者消费者问题的多线程互斥控制,源程序出自《Linux网络编程》

  */

  #include <stdio.h>

  #include <pthread.h>

  #include <sched.h>

  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 <stdio.h>

  #include <pthread.h>

  #include <semaphore.h>

  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 <stdio.h>

  #include <pthread.h>

  #include <sched.h>

  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);

  }

 推荐初学者一个编程技术的学习网站,96堆栈 软件编程网,http://www.96dz.com,里面有C++视频教程、C#视频教程、Java视频教程下载,还有C\C++、Java、C# .NET等编程技术文摘,包括目前主流的Linux编程与Web编程等学习资料视频教程下载。 

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
嵌入式系统,Linux,WinCE,单片机,ARM,DSP,EDA,FPGA,CPLD,PLC,数电,模电,PCB,硬件设计,软件编程,应用电子技术,视频教程、培训视频、教程资料、学习教程,海量资源下载中心, 海科资源
推荐文章
最近访客