http://blog.csdn.net/lzx_bupt/article/details/6915117
上篇說了下互斥量的用法,今兒說一下條件訊號量的用法,這兩種多線程變數的用法其實取決於情景,需要體會,見文:
[cpp] view plaincopy
- #include <iostream>
- #include <pthread.h>//帶標頭檔
- #include <stdio.h>
-
- using namespace std;
-
- #define BOUNDARY 5
-
- int tasks = 10;
-
- pthread_mutex_t tasks_mutex;//因為兩個線程要修改一個全域變數,需要互斥量;
- pthread_cond_t tasks_cond;//因為兩個線程間有條件關係:當tasks>5時,hello2處理它,處理一次減少1;反之hello1處理,直到tasks減為零;
-
- void* say_hello2(void* args)//hello2處理函數
- {
- pthread_t pid = pthread_self();//列印當前線程id便於跟蹤
- cout << "["<< pid << "] hello in thread " << *((int*)args) << endl;
-
- bool is_signaled = false;//隨便一個標誌位
- while(1)//無限迴圈
- {
- pthread_mutex_lock(&tasks_mutex);//要修改了,加鎖
- if (tasks > BOUNDARY)//>5才修改
- {
- cout << "["<< pid << "] take task: "<< tasks << " in thread "<< *((int*)args) << endl;
- --tasks;//減少1
- }
- else if (!is_signaled)
- {
- cout << "["<< pid << "] pthread_cond_signal in thread " << *((int*)args) << endl;
- pthread_cond_signal(&tasks_cond);//表明已經不是>5了告訴hello1進程去處理:發送訊號;
- is_signaled = true;//表示訊號已經發送了
- }
- pthread_mutex_unlock(&tasks_mutex);//操作完解鎖
-
- if (tasks == 0) break;//必須等待tasks全部減為零即hello1完成操作,才跳出迴圈結束這個進程
- }
- }
[cpp] view plaincopy
- <p>void* say_hello1(void* args)//<=5處理函數
- {
- pthread_t pid = pthread_self();
- cout << "["<< pid << "] hello in thread " << *((int*)args) << endl;</p><p> while(1)
- {
- pthread_mutex_lock(&tasks_mutex);
- if (tasks > BOUNDARY)//如果>5說明需要hello2處理,那麼該線程就需要等待
- {
- cout << "["<< pid << "] pthread_cond_wait in thread " << *((int*)args) << endl;
- pthread_cond_wait(&tasks_cond, &tasks_mutex);//等待訊號量生效,當hello2發出訊號,這裡就跳出wait,執行後續;
- }
- else
- {
- cout << "["<< pid << "] take task: "<< tasks << " in thread "<< *((int*)args) << endl;
- --tasks;//<=5就--
- }
- pthread_mutex_unlock(&tasks_mutex);</p><p> if (tasks == 0) break;//為零時退出,同hello2一樣
- }</p><p>}</p><p>int main()
- {
- pthread_attr_t attr;//線程建立為joinable的,使得主進程可以和兩個線程同步,兩個線程完成工作退出後,主進程再退出;
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);</p><p> pthread_mutex_init(&tasks_mutex, NULL);//初始化互斥量
- pthread_cond_init(&tasks_cond, NULL);//初始化條件訊號量</p><p> pthread_t tid1, tid2;//用於儲存兩個線程的id號
- int index1 = 1;
- int ret = pthread_create( &tid1, &attr, say_hello1, (void *)&index1);
- if (ret != 0)
- {
- cout << "pthread_create error: error_code=" << ret << endl;
- }</p><p> int index2 = 2;
- ret = pthread_create( &tid2, &attr, say_hello2, (void *)&index2);
- if (ret != 0)
- {
- cout << "pthread_create error: error_code=" << ret << endl;
- }</p><p> </p><p> pthread_join(tid1, NULL);//串連兩個線程
- pthread_join(tid2, NULL);</p><p> pthread_attr_destroy(&attr);//該銷毀的銷毀
- pthread_mutex_destroy(&tasks_mutex);
- pthread_cond_destroy(&tasks_cond);</p><p> //正常退出
- }</p><p> </p>
慣例:g++ -lpthread -o ex_cond ex_cond.cpp
執行結果:
[cpp] view plaincopy
- [cpp@node2 pthread]$ ./ex_cond
- [140009886947088] hello in thread 2
- [140009886947088] take task: 10 in thread 2
- [140009886947088] take task: 9 in thread 2
- [140009886947088] take task: 8 in thread 2
- [140009886947088] take task: 7 in thread 2
- [140009886947088] take task: 6 in thread 2
- [140009886947088] pthread_cond_signal in thread 2
- [140009897436944] hello in thread 1
- [140009897436944] take task: 5 in thread 1
- [140009897436944] take task: 4 in thread 1
- [140009897436944] take task: 3 in thread 1
- [140009897436944] take task: 2 in thread 1
- [140009897436944] take task: 1 in thread 1