linux下條件變數的使用

來源:互聯網
上載者:User

條件變數使我們可以睡眠等待某種條件出現。(windows下可以用互鎖函數類比linux下的條件變數)

條件變數是利用線程間共用的全域變數進行同步的一種機制,主要包括兩個動作:一個線程等待"條件變數的條件成立"而掛起;另一個線程使"條件成立"(給出條件成立訊號)。

為了防止競爭,條件變數的使用總是和一個互斥鎖結合在一起。 

條件變數類型為pthread_cond_  

條件變數和互斥鎖一樣,都有靜態動態兩種建立方式:

       靜態方式使用PTHREAD_COND_INITIALIZER常量,如下:pthread_cond_t cond=PTHREAD_COND_INITIALIZER  

       動態方式調用pthread_cond_init()函數,API定義如下:int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr)儘管POSIX標準中為條件變數定義了屬性,但在LinuxThreads中沒有實現,因此cond_attr值通常為NULL,且被忽略。

登出一個條件變數需要調用pthread_cond_destroy(),只有在沒有線程在該條件變數上等待的時候才能登出這個條件變數,否則返回EBUSY。API定義如下:int pthread_cond_destroy(pthread_cond_t *cond)

下面是鳥人從自己寫的程式中摘出來的條件變數使用代碼:

#include <stdlib.h>#include <queue>#include <pthread.h>struct InitIndexTask{int x;int y;int z;InitIndexTask(int a, int b, int c){this->x = a;this->y = b;this->z = c;}};/*******global variables******/const uint8_t THREAD_COUNT = 8;pthread_mutex_t init_mutex     = PTHREAD_MUTEX_INITIALIZER;pthread_cond_t  condition_init   = PTHREAD_COND_INITIALIZER;queue<InitIndexTask*> InitIndexQ;/*******init thread body******/void* InitIndexThread(void* data){char* thread_name = (char*)data;cout << "created an init_index_thread(tid:" << (unsigned int)pthread_self() << " tname:" << thread_name << ")." << endl;InitIndexTask* initTask = NULL;while (true){pthread_mutex_lock( &init_mutex );while (InitIndexQ.empty())pthread_cond_wait( &condition_init, &init_mutex );initTask = InitIndexQ.front();InitIndexQ.pop();pthread_mutex_unlock( &init_mutex );if (!initTask)break;//...delete initTask;}cout << thread_name << " exited." << endl;delete[] BUFF;delete[] thread_name;}/******* main******/int main(int argc, char** argv){pthread_t* threadArr = new pthread_t[THREAD_COUNT];for (uint8_t i = 0; i < THREAD_COUNT; ++i){char* thread_name = new char[50];snprintf(thread_name, 50, "Thread-%d", i);pthread_create(&threadArr[i], 0, &InitIndexThread, thread_name);}for (int i = 0; i < 100; i++) {InitIndexTask* task = new InitIndexTask(i, 2*i, 3*i);pthread_mutex_lock( &init_mutex );InitIndexQ.push(task);pthread_mutex_unlock( &init_mutex );pthread_cond_signal( &condition_init );}for (uint8_t i = 0; i < THREAD_COUNT; ++i){pthread_mutex_lock( &init_mutex );InitIndexQ.push(NULL);pthread_mutex_unlock( &init_mutex );pthread_cond_signal( &condition_init );}for (uint8_t i = 0; i < THREAD_COUNT; ++i)pthread_join(threadArr[i], NULL);delete[] threadArr;cout << "main thread exited." << endl;}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.