日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

调用另一个cpp的变量_再谈条件变量—从入门到出家

發(fā)布時(shí)間:2023/12/10 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 调用另一个cpp的变量_再谈条件变量—从入门到出家 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

再談條件變量—從入門到出家


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ù);
C++ 使用案例#include <iostream> #include <thread> #include <condition_variable> #include <mutex> #include <unistd.h>using namespace std;//全局條件變量 condition_variable cond; mutex _mutex; int count = 0;void fun1() {while(1){count++;unique_lock<mutex>lock(_mutex);if(count%5 == 0){cond.notify_one();}else{cout<<"this is fun1,count="<<count<<endl;}lock.unlock();sleep(1);} }void fun2() {while(1){unique_lock<mutex>lock(_mutex);cond.wait(lock);cout<<"this is fun2,count="<<count<<endl;lock.unlock();sleep(2);} }int main() {thread t1(fun1);thread t2(fun2);t1.join();t2.join();return 0; }

結(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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。