国产精品免费无遮挡无码永久视频-国产高潮视频在线观看-精品久久国产字幕高潮-国产精品99精品无码视亚

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.4huy16.com/thread-55992-1-1.html     【打印本頁】

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

廠商推薦

  • Microchip視頻專區
  • 電動兩輪車設計生態系統
  • 技術熱潮席卷三城,2025 Microchip中國技術精英年會圓滿收官!
  • 常見深度學習模型介紹及應用培訓教程
  • Microchip第22屆中國技術精英年會——采訪篇
  • 貿澤電子(Mouser)專區

相關視頻

關于我們  -  服務條款  -  使用指南  -  站點地圖  -  友情鏈接  -  聯系我們
電子工程網 © 版權所有   京ICP備16069177號 | 京公網安備11010502021702
快速回復 返回頂部 返回列表