国产毛片a精品毛-国产毛片黄片-国产毛片久久国产-国产毛片久久精品-青娱乐极品在线-青娱乐精品

Linux多線程編程的學習要點

發布時間:2011-2-25 19:42    發布者:hikesoso2010
關鍵詞: linux , 多線程編程
#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);  }
本文地址:http://www.qingdxww.cn/thread-55992-1-1.html     【打印本頁】

本站部分文章為轉載或網友發布,目的在于傳遞和分享信息,并不代表本網贊同其觀點和對其真實性負責;文章版權歸原作者及原出處所有,如涉及作品內容、版權和其它問題,我們將根據著作權人的要求,第一時間更正或刪除。
您需要登錄后才可以發表評論 登錄 | 立即注冊

廠商推薦

  • Microchip視頻專區
  • 利用模擬開發工具生態系統進行安全電路設計
  • 更佳設計的解決方案——Microchip模擬開發生態系統
  • 我們是Microchip
  • 深度體驗Microchip自動輔助駕駛應用方案——2025巡展開啟報名!
  • 貿澤電子(Mouser)專區

相關視頻

關于我們  -  服務條款  -  使用指南  -  站點地圖  -  友情鏈接  -  聯系我們
電子工程網 © 版權所有   京ICP備16069177號 | 京公網安備11010502021702
快速回復 返回頂部 返回列表
主站蜘蛛池模板: 成年一级片 | 久久8| 成年黄网站免费大全毛片 | 色资源二区在线视频 | 欧美日韩视频在线一区二区 | 亚洲综合精品一二三区在线 | 日韩国产欧美一区二区三区在线 | 国产三级一区二区三区 | 亚洲精品欧美综合四区 | www.日韩.com| 亚洲高清在线看 | 亚洲精品永久www忘忧草 | 欧美3p大片在线观看完整版 | 亚洲一区二区三区日本久久九 | 日韩高清专区 | 明星大侦探第四季免费可投屏 | 日本处888xxxx | 成年女人毛片免费观看不卡 | 毛片免费高清免费 | 亚洲免费视频网站 | 蜜臀91精品国产高清在线观看 | 香蕉在线精品视频在线观看2 | 国产午夜亚洲精品不卡免下载 | 亚洲视频观看 | 国产欧美一区二区久久 | 自拍偷拍 亚洲 | 99精品视频在线观看re | 久久国产精品99久久久久久老狼 | 99久久精品国内 | 日本高清色视频www 日本高清色本在线www游戏 | 精品国产一区二区三区精东影业 | 91热在线| 四虎4hu新地址入口 四虎4h | 亚洲国产高清在线观看视频 | 激情五月婷婷久久 | 99视频在线精品免费观看18 | 国产精品动漫视频网站 | 国产成人综合亚洲欧美在 | 尻逼小说 | 亚洲国产乱码在线精品 | 日本va在线观看 |