這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)atomic怎么在c++11項(xiàng)目中使用,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
std::atomic_flag是一個(gè)原子的布爾類型,可支持兩種原子操作:
test_and_set, 如果atomic_flag對象被設(shè)置,則返回true; 如果atomic_flag對象未被設(shè)置,則設(shè)置之,返回false
clear. 清楚atomic_flag對象
std::atomic_flag可用于多線程之間的同步操作,類似于linux中的信號(hào)量。使用atomic_flag可實(shí)現(xiàn)mutex.
#include <iostream> #include <atomic> #include <vector> #include <thread> #include <sstream> std::atomic_flag lock = ATOMIC_FLAG_INIT; std::stringstream stream; void append_numer(int x) { while (lock.test_and_set()); stream << "thread#" << x << "\n"; lock.clear(); } int main() { std::vector<std::thread> ths; for (int i=0; i<10; i++) ths.push_back(std::thread(append_numer, i)); for (int i=0; i<10; i++) ths[i].join(); std::cout << stream.str(); return 0; }
std::atomic對int, char, bool等數(shù)據(jù)結(jié)構(gòu)進(jìn)行原子性封裝,在多線程環(huán)境中,對std::atomic對象的訪問不會(huì)造成競爭-冒險(xiǎn)。利用std::atomic可實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)的無鎖設(shè)計(jì)。
#include <iostream> #include <atomic> #include <vector> #include <thread> #include <sstream> std::atomic<bool> ready(false); std::atomic_flag winner = ATOMIC_FLAG_INIT; void count1m(int i) { while (!ready); for (int i=0; i<1000000; i++); if (!winner.test_and_set()) std::cout << "winner: " << i << std::endl; } int main() { std::vector<std::thread> ths; for (int i=0; i<10; i++) ths.push_back(std::thread(count1m, i)); ready = true; for (int i=0; i<10; i++) ths[i].join(); return 0; }
在上例中,執(zhí)行read=true之后,所有線程結(jié)束空等。winner被初始化為ATOMIC_FLAG_INIT,最先執(zhí)行winner.test_and_set并返回false的線程為winner。
#include <iostream> #include <atomic> #include <vector> #include <thread> #include <sstream> std::atomic<int> foo(0); void set_foo(int x) { foo = x; } void print_foo() { while (foo == 0) { std::this_thread::yield(); } std::cout << "x: " << foo << std::endl; } int main() { std::thread print_th(print_foo); std::thread set_th(set_foo, 10); print_th.join(); set_th.join(); return 0; }
在上例總,set_foo用于設(shè)置atomic<int>對象的值,print_foo用于打印atomic<int>對象的值。std::atomic對象的值的讀取和寫入可使用load和store實(shí)現(xiàn)。
#include <iostream> #include <cassert> #include <atomic> #include <vector> #include <unistd.h> #include <thread> #include <sstream> std::atomic<int> foo(0); std::atomic_flag lock = ATOMIC_FLAG_INIT; void add_foo() { while (1) { foo++; // foo = foo + 1; while (lock.test_and_set()); std::cout <<"add: " << foo << std::endl; lock.clear(); usleep(1000); } } void sub_foo() { while (1) { foo--; // foo = foo - 1; while (lock.test_and_set()); std::cout << "sub: " << foo << std::endl; lock.clear(); usleep(1000); } } int main() { std::thread th3 = std::thread(add_foo); std::thread th2 = std::thread(sub_foo); th2.join(); th3.join(); return 0; }
上述就是小編為大家分享的atomic怎么在c++11項(xiàng)目中使用了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
本文標(biāo)題:atomic怎么在c++11項(xiàng)目中使用-創(chuàng)新互聯(lián)
文章位置:http://chinadenli.net/article10/dgphgo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、企業(yè)網(wǎng)站制作、域名注冊、響應(yīng)式網(wǎng)站、搜索引擎優(yōu)化、網(wǎng)站設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容