c++11 atomic的使用詳解
std::atomic_flag
std::atomic_flag是一個(gè)原子的布爾類(lèi)型,可支持兩種原子操作:
- test_and_set, 如果atomic_flag對(duì)象被設(shè)置,則返回true; 如果atomic_flag對(duì)象未被設(shè)置,則設(shè)置之,返回false
- clear. 清楚atomic_flag對(duì)象
std::atomic_flag可用于多線(xiàn)程之間的同步操作,類(lèi)似于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
std::atomic對(duì)int, char, bool等數(shù)據(jù)結(jié)構(gòu)進(jìn)行原子性封裝,在多線(xiàn)程環(huán)境中,對(duì)std::atomic對(duì)象的訪(fǎng)問(wèn)不會(huì)造成競(jìng)爭(zhēng)-冒險(xiǎn)。利用std::atomic可實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)的無(wú)鎖設(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之后,所有線(xiàn)程結(jié)束空等。winner被初始化為ATOMIC_FLAG_INIT,最先執(zhí)行winner.test_and_set并返回false的線(xiàn)程為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>對(duì)象的值,print_foo用于打印atomic<int>對(duì)象的值。std::atomic對(duì)象的值的讀取和寫(xiě)入可使用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 th2 = std::thread(add_foo);
std::thread th1 = std::thread(sub_foo);
th1.join();
th2.join();
return 0;
}
atomic<int>支持++和--的原子操作。
以上就是c++11 atomic的使用詳解的詳細(xì)內(nèi)容,更多關(guān)于c++11 atomic的使用的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(26.有序數(shù)組中去除重復(fù)項(xiàng))
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(26.有序數(shù)組中去除重復(fù)項(xiàng)),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++協(xié)程實(shí)現(xiàn)序列生成器的案例分享
序列生成器通常的實(shí)現(xiàn)是在一個(gè)協(xié)程內(nèi)部通過(guò)某種方式向外部傳一個(gè)值出去,并且將自己掛起,本文圍繞序列生成器這個(gè)經(jīng)典的協(xié)程案例介紹了協(xié)程的銷(xiāo)毀、co_await 運(yùn)算符、await_transform 以及 yield_value 的用法,需要的朋友可以參考下2024-05-05
VC++ 6.0 C語(yǔ)言實(shí)現(xiàn)俄羅斯方塊詳細(xì)教程
這篇文章主要為大家介紹了VC++ 6.0 C語(yǔ)言實(shí)現(xiàn)俄羅斯方塊詳細(xì)教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
C++中std::construct()與std::destroy()的使用
std::construct()和std::destroy()是C++ STL中的函數(shù)模板,用于在已分配的存儲(chǔ)區(qū)域中構(gòu)造或銷(xiāo)毀對(duì)象,本文主要介紹了C++中std::construct()與std::destroy()的使用,感興趣的可以了解一下2024-02-02
C++如何判斷一個(gè)數(shù)字是否為質(zhì)數(shù)
這篇文章主要為大家詳細(xì)介紹了C++如何判斷一個(gè)數(shù)字是否為質(zhì)數(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
C語(yǔ)言實(shí)現(xiàn)支持動(dòng)態(tài)拓展和銷(xiāo)毀的線(xiàn)程池
這篇文章主要為大家介紹了C語(yǔ)言實(shí)現(xiàn)支持動(dòng)態(tài)拓展和銷(xiāo)毀的線(xiàn)程池,感興趣的小伙伴們可以參考一下2016-01-01
QT中QStringListModel類(lèi)的應(yīng)用介紹
QStringListModel是最簡(jiǎn)單的模型類(lèi),具備向視圖提供字符串?dāng)?shù)據(jù)的能力,本文主要介紹了QT中QStringListModel類(lèi)的應(yīng)用介紹,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
c++文件監(jiān)控之FileSystemWatcher
為了監(jiān)控web程序的靜態(tài)文件是否被惡意改動(dòng),所以學(xué)習(xí)了一下FileSystemWatcher 類(lèi)對(duì)文件的監(jiān)控,由于還在初級(jí)階段,這里只貼一下關(guān)于FileSystemWatcher學(xué)習(xí)的一些代碼2019-04-04

