乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      多線程學(xué)習(xí)1

       ColonelLee 2011-07-17
      等待線程
      1。使用pthread_cond_wait前要先加鎖
      2。pthread_cond_wait內(nèi)部會解鎖,然后等待條件變量被其它線程激活
      3。pthread_cond_wait被激活后會再自動加鎖

      激活線程:
      1。加鎖(和等待線程用同一個(gè)鎖)
      2。pthread_cond_signal發(fā)送信號
      3。解鎖
      激活線程的上面三個(gè)操作在運(yùn)行時(shí)間上都在等待線程的pthread_cond_wait函數(shù)內(nèi)部。

      程序示例:

      #include <stdio.h>
      #include <pthread.h>
      #include <unistd.h>

      pthread_mutex_t count_lock;
      pthread_cond_t count_nonzero;
      unsigned count = 0;

      void * decrement_count(void *arg) {
      pthread_mutex_lock (&count_lock);
      printf("decrement_count get count_lock\n");
      while(count==0) {
      printf("decrement_count count == 0 \n");
      printf("decrement_count before cond_wait \n");
      pthread_cond_wait( &count_nonzero, &count_lock);
      printf("decrement_count after cond_wait \n");
      }
      count = count -1;
      pthread_mutex_unlock (&count_lock);
      }

      void * increment_count(void *arg){
      pthread_mutex_lock(&count_lock);
      printf("increment_count get count_lock\n");
      if(count==0) {
      printf("increment_count before cond_signal\n");
      pthread_cond_signal(&count_nonzero);
      printf("increment_count after cond_signal\n");
      }
      count=count+1;
      pthread_mutex_unlock(&count_lock);
      }

      int main(void)
      {
      pthread_t tid1,tid2;

      pthread_mutex_init(&count_lock,NULL);
      pthread_cond_init(&count_nonzero,NULL);

      pthread_create(&tid1,NULL,decrement_count,NULL);
      sleep(2);
      pthread_create(&tid2,NULL,increment_count,NULL);

      sleep(10);
      pthread_exit(0);
      }
       
       
      2.decrement_count和increment_count在兩個(gè)線程A和B中被調(diào)用。
      正確的情況下,如果decrement_count首先運(yùn)行,那么A會被阻塞到pthread_cond_wait。隨后increment_count運(yùn)行,它調(diào)用pthread_cond_signal喚醒等待條件鎖count_nonzero的A線程,但是A線程并不會馬上執(zhí)行,因?yàn)樗貌坏交コ怄icount_lock。當(dāng)B線程執(zhí)行pthread_mutex_unlock之后A線程才得以繼續(xù)執(zhí)行。
       
      如果pthread_cond_signal前后沒有使用互斥鎖count_lock保護(hù),可能的情況是這樣。A阻塞到pthread_cond_wait,然后B執(zhí)行到pthread_cond_signal時(shí)候,發(fā)生了線程切換,于是A被喚醒,并且發(fā)現(xiàn)count依然是0,所以繼續(xù)阻塞到條件鎖count_nonzero上。然后B繼續(xù)執(zhí)行,這時(shí)候盡管count=1,A永遠(yuǎn)不會被喚醒了。這樣就發(fā)生了邏輯錯誤。
       
      當(dāng)然在這個(gè)上下文中,如果把count=count+1放在函數(shù)放在pthread_cond_signal之前變成
       
      increment_count(){
           count=count+1;
       
          if(count==0)
              pthread_cond_signal(&count_nonzero);
      }
       
      這樣沒有問題。但是這種方法并不能保證所有情況下都適用。于是需要用互斥鎖保護(hù)條件鎖相關(guān)的變量。也就是說條件鎖是用來線程通訊的,但是互斥鎖是為了保護(hù)這種通訊不會產(chǎn)生邏輯錯誤,可以正常工作。
       

        本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多