<var id="fnfpo"><source id="fnfpo"></source></var>
<rp id="fnfpo"></rp>

<em id="fnfpo"><object id="fnfpo"><input id="fnfpo"></input></object></em>
<em id="fnfpo"><acronym id="fnfpo"></acronym></em>
  • <th id="fnfpo"><track id="fnfpo"></track></th>
  • <progress id="fnfpo"><track id="fnfpo"></track></progress>
  • <tbody id="fnfpo"><pre id="fnfpo"></pre></tbody>

  • x
    x

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

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

    廠商推薦

    • Microchip視頻專區
    • EtherCAT®和Microchip LAN925x從站控制器介紹培訓教程
    • MPLAB®模擬設計器——在線電源解決方案,加速設計
    • 讓您的模擬設計靈感,化為觸手可及的現實
    • 深度體驗Microchip自動輔助駕駛應用方案——2025巡展開啟報名!
    • 貿澤電子(Mouser)專區

    相關視頻

    關于我們  -  服務條款  -  使用指南  -  站點地圖  -  友情鏈接  -  聯系我們
    電子工程網 © 版權所有   京ICP備16069177號 | 京公網安備11010502021702
    快速回復 返回頂部 返回列表
    精品一区二区三区自拍图片区_国产成人亚洲精品_亚洲Va欧美va国产综合888_久久亚洲国产精品五月天婷