国产毛片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)者問題的信號(hào)量控制,可以與上述程序進(jìn)行對(duì)比,出處--同上  */  #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); /*信號(hào)量初始化*/  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); /*銷毀信號(hào)量*/  return 0;  }  void *producter_f(void *arg)  {  int semval = 0; /*信號(hào)量的初始值*/  while (running)  {  usleep(1);  sem_post(&sem); /*信號(hào)量+1*/  sem_getvalue(&sem, &semval);/*得到信號(hào)量的值*/  printf("pro,num:%d\n",semval);  }  }  void *consumer_f(void *arg)  {  int semval = 0;  while (running)  {  usleep(1);  sem_wait(&sem); /*信號(hào)量-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://www.qingdxww.cn/thread-55992-1-1.html     【打印本頁(yè)】

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

廠商推薦

  • Microchip視頻專區(qū)
  • 5分鐘詳解定時(shí)器/計(jì)數(shù)器E和波形擴(kuò)展!
  • 了解一下Microchip強(qiáng)大的PIC18-Q24 MCU系列
  • 無線充電基礎(chǔ)知識(shí)及應(yīng)用培訓(xùn)教程3
  • 為何選擇集成電平轉(zhuǎn)換?
  • 貿(mào)澤電子(Mouser)專區(qū)

相關(guān)視頻

關(guān)于我們  -  服務(wù)條款  -  使用指南  -  站點(diǎn)地圖  -  友情鏈接  -  聯(lián)系我們
電子工程網(wǎng) © 版權(quán)所有   京ICP備16069177號(hào) | 京公網(wǎng)安備11010502021702
快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 亚洲 欧美 自拍 明星合成 | 国内一级特黄女人精品片 | 播放毛片 | 欧美在线观看视频一区 | 不卡中文一二三区 | 欧美日韩一区在线观看 | 亚洲国产字幕 | 亚洲欧美中文日韩在线v日本 | 精品视频一区二区三三区四区 | 精品欧美日韩一区二区 | 亚洲日韩中文字幕一区 | 免费国产午夜高清在线视频 | 欧美成人高清在线视频大全 | 亚洲欧美视频在线播放 | 精品中文字幕一区二区三区四区 | 免费国产99久久久香蕉 | 粉嫩极品国产在线观看 | 韩国欧洲一级毛片免费 | 欧美精品四虎在线观看 | 国产精品va一级二级三级 | 久久亚洲免费视频 | 日本人成动漫网站在线观看 | 一级做a爰性色毛片免费 | 午夜在线视频网站 | 一区二区三区日韩精品 | 婷婷综合网站 | 日韩毛片在线看 | 色综合综合色综合色综合 | 日本高清在线视频www色下载 | 欧美日韩国内 | 日韩视频一区二区在线观看 | 免费日本黄色 | 四虎8848| 免费手机黄色网站 | 91欧美视频| 天天干天天操天天添 | 亚洲一区二区天海翼 | 日韩中文字幕视频 | 91精品国产麻豆国产自产在线 | 日本特黄特色aaa大片免费 | aa级国产女人毛片水真多 |