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

Linux多線程編程的學(xué)習(xí)要點(diǎn)

發(fā)布時(shí)間:2011-2-25 19:42    發(fā)布者:hikesoso2010
關(guān)鍵詞: linux , 多線程編程
#include   #include   #include   void *producter_f(void *arg);  void *consumer_f(void *arg);  int buffer_has_item = 0; /*設(shè)置緩存數(shù)量*/  pthread_mutex_t mutex; /*設(shè)置互斥*/  int running = 1;  int main (void)  {  pthread_t consumer_t; /*線程參數(shù)*/  pthread_t producter_t;  /*不知道這句為什么不給我變藍(lán)色,初始化互斥*/  pthread_mutex_init(&mutex, NULL);  /*創(chuàng)建線程*/  pthread_create(&producter_t, NULL, (void *)producter_f, NULL);  pthread_create(&consumer_t, NULL, (void *)consumer_f, NULL);  usleep(1);  running = 0;  /*等待線程退出,一個(gè)線程不能夠被多個(gè)線程等待*/  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); /*加鎖,進(jìn)入互斥區(qū)*/  buffer_has_item++;  printf("product ,num:%d\n", buffer_has_item);  pthread_mutex_unlock(&mutex); /*解鎖,離開互斥區(qū)*/  }  }  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);  }  }  /*  *生產(chǎn)者消費(fèi)者問題的信號量控制,可以與上述程序進(jìn)行對比,出處--同上  */  #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);  }  }  /*  *條件變量來控制線程的同步,根據(jù)百度百科代碼改編而成,不肯定沒有錯(cuò)誤  */  #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)  {/*使線程阻塞在一個(gè)條件變量上線程可以被函數(shù)pthread_cond_signal函數(shù)喚醒*/  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上的一個(gè)線程*/  pthread_cond_signal(&count_nonzero);  }  count = count + 1;  pthread_mutex_unlock(&count_lock);  }
本文地址:http://m.qingdxww.cn/thread-55992-1-1.html     【打印本頁】

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

廠商推薦

  • Microchip視頻專區(qū)
  • 想要避免發(fā)生災(zāi)難,就用MPLAB® SiC電源仿真器!
  • PIC18-Q71系列MCU概述
  • 了解一下Microchip強(qiáng)大的PIC18-Q24 MCU系列
  • 無線充電基礎(chǔ)知識及應(yīng)用培訓(xùn)教程2
  • 貿(mào)澤電子(Mouser)專區(qū)

相關(guān)視頻

關(guān)于我們  -  服務(wù)條款  -  使用指南  -  站點(diǎn)地圖  -  友情鏈接  -  聯(lián)系我們
電子工程網(wǎng) © 版權(quán)所有   京ICP備16069177號 | 京公網(wǎng)安備11010502021702
快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 日韩特级片 | 国产精品久久久香蕉 | 91啦中文成人 | 精品欧美日韩一区二区三区 | 亚洲黄色在线播放 | 欧美成a人免费观看 | 老师邪恶影院a啦啦啦影院 老师让我她我爽了好久老 老师感受到它在你里面了吗app | 精品日本三级在线观看视频 | 91热久久免费精品99 | 精品视频香蕉尹人在线 | 国产99精品视频 | 久久精品国产99久久久 | 日本国产成人精品视频 | 欧美精品一区二区久久 | 亚洲1024 | 91高清完整版在线观看 | 草莓视频app无限观看 | 春日迟迟再出发最新一期在线观看 | 国产小视频在线播放 | 国家免费看在线视频 | 啪影院免费线在线观看 | 欧美在线视频导航 | 四虎播播| 国产一区二区三区亚洲综合 | 99re在线这里只有精品 | 国产精品中文字幕在线观看 | 国产高清av在线播放 | h视频在线看 | 亚洲日本在线免费观看 | 日本特黄乱人伦片 | 日本一区二区三区欧美在线观看 | 在线观看国产精品入口 | a性片| 青青自拍视频一区二区三区 | 成人精品一区二区久久 | 中文字幕欧美日韩高清 | 一二三四在线社区 | 欧美一级成人 | 日本a免费观看 | 亚洲视频二 | 欧美日韩第一区 |