亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C++ Boost Lockfree超詳細(xì)講解使用方法

 更新時間:2022年11月20日 10:29:40   作者:無水先生  
Boost是為C++語言標(biāo)準(zhǔn)庫提供擴(kuò)展的一些C++程序庫的總稱。Boost庫是一個可移植、提供源代碼的C++庫,作為標(biāo)準(zhǔn)庫的后備,是C++標(biāo)準(zhǔn)化進(jìn)程的開發(fā)引擎之一,是為C++語言標(biāo)準(zhǔn)庫提供擴(kuò)展的一些C++程序庫的總稱

Boost.Lockfree

一、說明

Boost.Lockfree 提供線程安全和無鎖容器。可以從多個線程訪問此庫中的容器,而無需同步訪問。

在 1.56.0 版本中,Boost.Lockfree 只提供了兩個容器:boost::lockfree::queue 類型的隊列和 boost::lockfree::stack 類型的棧。對于隊列,可以使用第二個實(shí)現(xiàn):boost::lockfree::spsc_queue。此類針對只有一個線程寫入隊列和只有一個線程從隊列讀取的用例進(jìn)行了優(yōu)化。類名中的縮寫 spsc 代表單一生產(chǎn)者/單一消費(fèi)者。

二、示例和代碼

示例 46.1。使用 boost::lockfree::spsc_queue

#include <boost/lockfree/spsc_queue.hpp>
#include <thread>
#include <iostream>
boost::lockfree::spsc_queue<int> q{100};
int sum = 0;
void produce()
{
  for (int i = 1; i <= 100; ++i)
    q.push(i);
}
void consume()
{
  int i;
  while (q.pop(i))
    sum += i;
}
int main()
{
  std::thread t1{produce};
  std::thread t2{consume};
  t1.join();
  t2.join();
  consume();
  std::cout << sum << '\n';
}

Example46.1

示例 46.1 使用容器 boost::lockfree::spsc_queue。第一個執(zhí)行函數(shù) produce() 的線程將數(shù)字 1 到 100 添加到容器中。第二個線程執(zhí)行 consume(),從容器中讀取數(shù)字并將它們相加。因?yàn)槿萜?boost::lockfree::spsc_queue 明確支持來自兩個線程的并發(fā)訪問,所以不需要同步線程。

請注意,函數(shù) consume() 會在線程終止后被第二次調(diào)用。這需要計算所有 100 個數(shù)字的總數(shù),即 5050。因?yàn)?consume() 在循環(huán)中訪問隊列,它讀取數(shù)字的速度可能比 produce() 插入數(shù)字的速度快。如果隊列為空,pop() 返回 false。因此,執(zhí)行 consume() 的線程可能會終止,因?yàn)榱硪粋€線程中的 produce() 無法足夠快地填充隊列。如果執(zhí)行 produce() 的線程終止,那么很明顯所有數(shù)字都已添加到隊列中。第二次調(diào)用 consume() 確保將可能尚未讀取的數(shù)字添加到 sum 中。

隊列的大小被傳遞給構(gòu)造函數(shù)。因?yàn)?boost::lockfree::spsc_queue 是用循環(huán)緩沖區(qū)實(shí)現(xiàn)的,所以示例 46.1 中的隊列容量為 100 個元素。如果由于隊列已滿而無法添加值,則 push() 返回 false。該示例不檢查 push() 的返回值,因?yàn)榍『糜?100 個數(shù)字被添加到隊列中。因此,100 個元素就足夠了。

示例 46.2。 boost::lockfree::spsc_queue 和 boost::lockfree::capacity

#include <boost/lockfree/spsc_queue.hpp>
#include <boost/lockfree/policies.hpp>
#include <thread>
#include <iostream>
using namespace boost::lockfree;
spsc_queue<int, capacity<100>> q;
int sum = 0;
void produce()
{
  for (int i = 1; i <= 100; ++i)
    q.push(i);
}
void consume()
{
  while (q.consume_one([](int i){ sum += i; }))
    ;
}
int main()
{
  std::thread t1{produce};
  std::thread t2{consume};
  t1.join();
  t2.join();
  q.consume_all([](int i){ sum += i; });
  std::cout << sum << '\n';
}

Example46.2

示例 46.2 與前面的示例類似,但這次循環(huán)緩沖區(qū)的大小是在編譯時設(shè)置的。這是通過模板 boost::lockfree::capacity 完成的,它需要容量作為模板參數(shù)。 q 是用默認(rèn)構(gòu)造函數(shù)實(shí)例化的——容量不能在運(yùn)行時設(shè)置。

函數(shù) consume() 已更改為使用 consume_one() 而不是 pop() 來讀取數(shù)字。 lambda 函數(shù)作為參數(shù)傳遞給 consume_one()。 consume_one() 就像 pop() 一樣讀取一個數(shù)字,但該數(shù)字不是通過對調(diào)用者的引用返回的。它作為唯一參數(shù)傳遞給 lambda 函數(shù)。

當(dāng)線程終止時,main() 調(diào)用成員函數(shù) consume_all(),而不是 consume()。 consume_all() 的工作方式與 consume_one() 類似,但要確保隊列在調(diào)用后為空。只要隊列中有元素,consume_all() 就會調(diào)用 lambda 函數(shù)。

示例 46.2 再次將 5050 寫入標(biāo)準(zhǔn)輸出。

示例 46.3。具有可變?nèi)萜鞔笮〉?boost::lockfree::queue

#include <boost/lockfree/queue.hpp>
#include <thread>
#include <atomic>
#include <iostream>
boost::lockfree::queue<int> q{100};
std::atomic<int> sum{0};
void produce()
{
  for (int i = 1; i <= 10000; ++i)
    q.push(i);
}
void consume()
{
  int i;
  while (q.pop(i))
    sum += i;
}
int main()
{
  std::thread t1{produce};
  std::thread t2{consume};
  std::thread t3{consume};
  t1.join();
  t2.join();
  t3.join();
  consume();
  std::cout << sum << '\n';
}

Example46.3

示例 46.3 在兩個線程中執(zhí)行 consume()。因?yàn)橛卸鄠€線程從隊列中讀取,所以不得使用類 boost::lockfree::spsc_queue。此示例改為使用 boost::lockfree::queue。

多虧了 std::atomic,對變量 sum 的訪問現(xiàn)在也是線程安全的。

隊列的大小設(shè)置為 100——這是傳遞給構(gòu)造函數(shù)的參數(shù)。但是,這只是初始大小。默認(rèn)情況下,boost::lockfree::queue 不使用循環(huán)緩沖區(qū)實(shí)現(xiàn)。如果添加到隊列中的項(xiàng)目多于設(shè)置的容量,則會自動增加。如果初始大小不夠,boost::lockfree::queue 會動態(tài)分配額外的內(nèi)存。

這意味著 boost::lockfree::queue 不一定是無鎖的。 boost::lockfree::queue 默認(rèn)使用的分配器是 boost::lockfree::allocator,它基于 std::allocator。因此,此分配器確定 boost::lockfree::queue 是否是無約束的無鎖。

#include <boost/lockfree/queue.hpp>
#include <thread>
#include <atomic>
#include <iostream>
using namespace boost::lockfree;
queue<int, fixed_sized<true>> q{10000};
std::atomic<int> sum{0};
void produce()
{
  for (int i = 1; i <= 10000; ++i)
    q.push(i);
}
void consume()
{
  int i;
  while (q.pop(i))
    sum += i;
}
int main()
{
  std::thread t1{produce};
  std::thread t2{consume};
  std::thread t3{consume};
  t1.join();
  t2.join();
  t3.join();
  consume();
  std::cout << sum << '\n';
}

Example46.4

示例 46.3 在兩個線程中執(zhí)行 consume()。因?yàn)橛卸鄠€線程從隊列中讀取,所以不得使用類 boost::lockfree::spsc_queue。此示例改為使用 boost::lockfree::queue。

多虧了 std::atomic,對變量 sum 的訪問現(xiàn)在也是線程安全的。

隊列的大小設(shè)置為 100——這是傳遞給構(gòu)造函數(shù)的參數(shù)。但是,這只是初始大小。默認(rèn)情況下,boost::lockfree::queue 不使用循環(huán)緩沖區(qū)實(shí)現(xiàn)。如果添加到隊列中的項(xiàng)目多于設(shè)置的容量,則會自動增加。如果初始大小不夠,boost::lockfree::queue 會動態(tài)分配額外的內(nèi)存。

這意味著 boost::lockfree::queue 不一定是無鎖的。 boost::lockfree::queue 默認(rèn)使用的分配器是 boost::lockfree::allocator,它基于 std::allocator。因此,此分配器確定 boost::lockfree::queue 是否是無約束的無鎖。

示例 46.4。具有恒定容器大小的 boost::lockfree::queue

到此這篇關(guān)于C++ Boost Lockfree超詳細(xì)講解使用方法的文章就介紹到這了,更多相關(guān)C++ Boost Lockfree內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論