国产毛片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://m.qingdxww.cn/thread-55992-1-1.html     【打印本頁】

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

廠商推薦

  • Microchip視頻專區
  • Dev Tool Bits——使用MPLAB® Discover瀏覽資源
  • Dev Tool Bits——使用條件軟件斷點宏來節省時間和空間
  • Dev Tool Bits——使用DVRT協議查看項目中的數據
  • Dev Tool Bits——使用MPLAB® Data Visualizer進行功率監視
  • 貿澤電子(Mouser)專區

相關視頻

關于我們  -  服務條款  -  使用指南  -  站點地圖  -  友情鏈接  -  聯系我們
電子工程網 © 版權所有   京ICP備16069177號 | 京公網安備11010502021702
快速回復 返回頂部 返回列表
主站蜘蛛池模板: 日本九九精品一区二区 | 欧洲在线观看在线视频吗 | 天天摸夜夜摸成人免费视频 | 在线观看不卡一区 | 日韩黄色在线 | 亚洲国产精品欧美日韩一区二区 | 四虎成人精品 | 一级毛片免费 | 亚洲视频免费观看 | 91国高清视频 | 在线观看日本 | 亚洲精品一二三四 | 草久网| 午夜爽爽影院 | 久久曰视频 | 欧美精品亚洲精品日韩经典 | 国产精品亚洲国产 | 就操网 | 草莓香蕉绿巨人丝瓜榴莲18 | 国产精品日本欧美一区二区 | 在线免费毛片 | 综合色网站 | 欧美va亚洲va香蕉在线 | 国产一级视频久久 | 九九视频在线播放 | 国产在线成人一区二区 | 亚洲最大福利 | 国产精品欧美亚洲韩国日本不卡 | 不卡中文字幕 | 国产成在线人视频免费视频 | 黄色网址中文字幕 | 在线播放三级 | 污网站免费观看 | 亚洲精品综合久久中文字幕 | 五月天婷婷网亚洲综合在线 | 国产精品亚洲综合天堂夜夜 | 欧美一区二区三区综合色视频 | 欧美成人精品一区二三区在线观看 | 女人18毛片特级一级免费视频 | 激情五月婷婷久久 | 久久成人动漫 |