调用另一个cpp的变量_再谈条件变量—从入门到出家
再談條件變量—從入門到出家
C語言--條件變量
條件變量是在線程中以睡眠的方式等待某一條件的發(fā)生;
條件變量是利用線程間共享的全局變量進(jìn)行同步的一種機(jī)制:
- 一個(gè)線程等待"條件變量的條件成立"掛起
- 另一個(gè)線程使"條件成立"
條件變量的使用總是和一個(gè)互斥鎖結(jié)合在一起;
**作用:**使用條件變量可以以原子方式阻塞線程,直到某個(gè)特定條件為真為止
我們一般使用的函數(shù)是:
#include <semaphore.h> int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) ; int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); int pthread_cond_signal(pthread_cond_t *cond); int pthread_cond_broadcast(pthread_cond_t *cond); int pthread_cond_destroy(pthread_cond_t *cond); 案例一代碼:
#include <stdio.h> #include <semaphore.h> #include <pthread.h>pthread_t t1; pthread_t t2;pthread_mutex_t mutex; pthread_cond_t cond;int i=0;void* Process1(void* arg) {while(1){pthread_mutex_lock(&mutex);i++;if(i%5 == 0){pthread_cond_signal(&cond);}else{printf("this is Process1n");}pthread_mutex_unlock(&mutex);sleep(2);} }void* Process2(void* arg) {while(1){pthread_mutex_lock(&mutex);pthread_cond_wait(&cond,&mutex);printf("this is Process2,i=%dn",i);pthread_mutex_unlock(&mutex);sleep(2);} }int main() {pthread_cond_init(&cond,NULL);pthread_mutex_init(&mutex,NULL);pthread_create(&t1,NULL,Process1,NULL);pthread_create(&t2,NULL,Process2,NULL);pthread_join(t1,NULL);pthread_join(t2,NULL);return 0; }結(jié)果:
root@iZuf67on1pthsuih96udyfZ:~/C++/C++_test/Progress/cond# ./test1 this is Process1 this is Process1 this is Process1 this is Process1 this is Process2,i=5 this is Process1 this is Process1 this is Process1 this is Process1 this is Process2,i=10 this is Process1 this is Process1 ^C通過條件變量來控制線程的輸出;
上述是在C語言中使用條件變量對線程進(jìn)行一個(gè)控制,在C++中,在標(biāo)準(zhǔn)庫中也提供了同樣的機(jī)制,使用起來會(huì)比C語言的更加方便,但是原理還是一樣的;
C++ 條件變量C++標(biāo)準(zhǔn)庫在< condition_variable >中
原則與C語言中類似
- 包含< mutex >和< condition_variable >,聲明一個(gè)mutex和一個(gè)condition_variable變量
- 通知“條件已滿足”的線程必須調(diào)用notify_one()或notify_all(),條件滿足時(shí)喚醒處于等待中的一個(gè)條件變量;
- 等待"條件被滿足"的線程必須調(diào)用wait(),可以讓線程在條件未被滿足時(shí)陷入休眠狀態(tài),當(dāng)接收到通知時(shí)被喚醒去處理相應(yīng)的任務(wù);
結(jié)果:
root@iZuf67on1pthsuih96udyfZ:~/C++/C++_test/Progress/cond# g++ -std=c++11 test2.cpp -o test2 -lpthread root@iZuf67on1pthsuih96udyfZ:~/C++/C++_test/Progress/cond# ./test2 this is fun1,count=1 this is fun1,count=2 this is fun1,count=3 this is fun1,count=4 this is fun2,count=5 this is fun1,count=6 this is fun1,count=7 this is fun1,count=8 ^C到這里,基本回顧了C和C++中條件變量的用法,但是在實(shí)際應(yīng)用中。又有設(shè)么用呢?
可能工作年限多的人,接觸到的比較多,但是對于很多小白來說,或者剛畢業(yè)、剛參加工作的人來說,這點(diǎn)接觸的就不是很多了,。這里來舉一個(gè)例子說一下:
假如在某個(gè)項(xiàng)目中,需要用到的是多個(gè)線程,最簡單的就是,用隊(duì)列的時(shí)候,我們一邊寫一邊讀,總是要加鎖的,但是,我們的線程就必須一直空轉(zhuǎn)對隊(duì)列進(jìn)行檢測是否有數(shù)據(jù),但是這樣往往會(huì)造成CPU使用率比較高,資源的浪費(fèi),這種情況下,我們就可以是使用條件變量,控制線程的循環(huán),降低資源使用率;當(dāng)然,也有很多場景值得去探索,也有很多技術(shù)可以解決這這個(gè)問題,希望大家一起探索前進(jìn);
問題思考上面介紹了C和C++中的使用,但是其實(shí)還是有些疑問的
疑問:為什么pthread_cond_wait前加了鎖,但是pthread_cond_signal還可以加鎖?首先看下我從網(wǎng)上找的一張圖:
從這個(gè)圖片中,我們發(fā)現(xiàn)了一個(gè)現(xiàn)象,pthread_cond_wait函數(shù)內(nèi)存進(jìn)行對鎖的解鎖,并且阻塞休眠操作,阻塞完成后,再次進(jìn)行了加鎖操作;也就是在pthread_cond_wait阻塞期間,pthread_cond_signal可以進(jìn)行加鎖和解鎖操作,這里是不沖突的;
往期精彩文章匯總- muduo源碼剖析學(xué)習(xí)總結(jié)
- 掌握這個(gè)技能,你可以暢游github
- C++ 簡單對象池實(shí)現(xiàn)
- 內(nèi)存池設(shè)計(jì)與實(shí)現(xiàn)
- 恭喜你!發(fā)現(xiàn)寶藏一份--技術(shù)文章匯總
想了解學(xué)習(xí)更多C++后臺(tái)服務(wù)器方面的知識(shí),請關(guān)注: 微信公眾號(hào):====CPP后臺(tái)服務(wù)器開發(fā)====
冰凍三尺,非一日之寒,水滴石穿,非一日之功,愿我們一起加油努力~
總結(jié)
以上是生活随笔為你收集整理的调用另一个cpp的变量_再谈条件变量—从入门到出家的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 少了榜一大哥 主播还能年入百万吗?专家:
- 下一篇: mfc cimage加载显示图片_在微信