<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                [TOC] # 簡介 與互斥鎖不同,條件變量是用來等待而不是用來上鎖的,**條件變量本身不是鎖**! 條件變量用來自動阻塞一個線程,直到某特殊情況發生為止。通常條件變量和互斥鎖同時使用。 條件變量的兩個動作: * 條件不滿, 阻塞線程 * 當條件滿足, 通知阻塞的線程開始工作 條件變量的類型: pthread\_cond\_t。 # pthread_cond_init函數 ~~~ #include <pthread.h> ? int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr); 功能: 初始化一個條件變量 參數: cond:指向要初始化的條件變量指針。 attr:條件變量屬性,通常為默認值,傳NULL即可 也可以使用靜態初始化的方法,初始化條件變量: pthread_cond_t cond = PTHREAD_COND_INITIALIZER; 返回值: 成功:0 失敗:非0錯誤號 ~~~ # pthread_cond_destroy函數 ~~~ #include <pthread.h> ? int pthread_cond_destroy(pthread_cond_t *cond); 功能: 銷毀一個條件變量 參數: cond:指向要初始化的條件變量指針 返回值: 成功:0 失敗:非0錯誤號 ~~~ # pthread_cond_wait函數 ~~~ #include <pthread.h> ? int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex); 功能: 阻塞等待一個條件變量 a) 阻塞等待條件變量cond(參1)滿足 b) 釋放已掌握的互斥鎖(解鎖互斥量)相當于pthread_mutex_unlock(&mutex); a) b) 兩步為一個原子操作。 c) 當被喚醒,pthread_cond_wait函數返回時,解除阻塞并重新申請獲取互斥鎖pthread_mutex_lock(&mutex); ? 參數: cond:指向要初始化的條件變量指針 mutex:互斥鎖 ? 返回值: 成功:0 失敗:非0錯誤號 ~~~ ~~~ int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct .*restrict abstime); 功能: 限時等待一個條件變量 ? 參數: cond:指向要初始化的條件變量指針 mutex:互斥鎖 abstime:絕對時間 ? 返回值: 成功:0 失敗:非0錯誤號 ~~~ abstime補充說明: ~~~ struct timespec { time_t tv_sec; /* seconds */ // 秒 long tv_nsec; /* nanosecondes*/ // 納秒 } ? time_t cur = time(NULL); //獲取當前時間。 struct timespec t; //定義timespec 結構體變量t t.tv_sec = cur + 1; // 定時1秒 pthread_cond_timedwait(&cond, &t); ~~~ # pthread_cond_signal函數 喚醒至阻塞在條件變量上的線程 ~~~ #include <pthread.h> ? int pthread_cond_signal(pthread_cond_t *cond); 功能: 喚醒至少一個阻塞在條件變量上的線程 參數: cond:指向要初始化的條件變量指針 返回值: 成功:0 失敗:非0錯誤號 ? int pthread_cond_broadcast(pthread_cond_t *cond); 功能: 喚醒全部阻塞在條件變量上的線程 參數: cond:指向要初始化的條件變量指針 返回值: 成功:0 失敗:非0錯誤號 ~~~ # 生產者消費者條件變量模型 線程同步典型的案例即為生產者消費者模型,而借助條件變量來實現這一模型,是比較常見的一種方法。 假定有兩個線程,一個模擬生產者行為,一個模擬消費者行為。兩個線程同時操作一個共享資源(一般稱之為匯聚),生產向其中添加產品,消費者從中消費掉產品。 ~~~ // 節點結構 typedef struct node { int data; struct node* next; }Node; ? // 永遠指向鏈表頭部的指針 Node* head = NULL; ? // 線程同步 - 互斥鎖 pthread_mutex_t mutex; // 阻塞線程 - 條件變量類型的變量 pthread_cond_t cond; ? // 生產者 void* producer(void* arg) { while (1) { // 創建一個鏈表的節點 Node* pnew = (Node*)malloc(sizeof(Node)); // 節點的初始化 pnew->data = rand() % 1000; // 0-999 ? // 使用互斥鎖保護共享數據 pthread_mutex_lock(&mutex); // 指針域 pnew->next = head; head = pnew; printf("====== produce: %lu, %d\n", pthread_self(), pnew->data); pthread_mutex_unlock(&mutex); ? // 通知阻塞的消費者線程,解除阻塞 pthread_cond_signal(&cond); ? sleep(rand() % 3); } return NULL; } ? void* customer(void* arg) { while (1) { pthread_mutex_lock(&mutex); // 判斷鏈表是否為空 if (head == NULL) { // 線程阻塞 // 該函數會對互斥鎖解鎖 pthread_cond_wait(&cond, &mutex); // 解除阻塞之后,對互斥鎖做加鎖操作 } // 鏈表不為空 - 刪掉一個節點 - 刪除頭結點 Node* pdel = head; head = head->next; printf("------ customer: %lu, %d\n", pthread_self(), pdel->data); free(pdel); pthread_mutex_unlock(&mutex); } return NULL; } ? int main(int argc, const char* argv[]) { pthread_t p1, p2; // init pthread_mutex_init(&mutex, NULL); pthread_cond_init(&cond, NULL); ? // 創建生產者線程 pthread_create(&p1, NULL, producer, NULL); // 創建消費者線程 pthread_create(&p2, NULL, customer, NULL); ? // 阻塞回收子線程 pthread_join(p1, NULL); pthread_join(p2, NULL); ? pthread_mutex_destroy(&mutex); pthread_cond_destroy(&cond); ? return 0; } ~~~ # 條件變量的優缺點 相較于mutex而言,條件變量可以減少競爭。 如直接使用mutex,除了生產者、消費者之間要競爭互斥量以外,消費者之間也需要競爭互斥量,但如果匯聚(鏈表)中沒有數據,消費者之間競爭互斥鎖是無意義的。 有了條件變量機制以后,只有生產者完成生產,才會引起消費者之間的競爭。提高了程序效率。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看