使用 shared ptr 執行緒間共享配置

2021-09-18 02:19:59 字數 4628 閱讀 2372

多個執行緒需要共享同乙份配置,但是配置本身又需要更新,如何處理?比較理想的處理方式是 copy on write,每次更新在原有的資料上拷貝乙份,加上更新,然後再替換全域性的那份配置。這樣做的好處是讀取配置完全不會被後台更新block住。要是用加鎖的方式,萬一後台更新執行緒**不靠譜,前台讀配置的地方就被block住了。但是這樣就帶來了乙個舊的配置物件什麼時候被**的問題。因為舊的配置物件可能被任意個執行緒在讀取著,我們怎麼知道什麼時候這些執行緒都讀完了呢?也許我們會得出乙個結論,這個做法只在有gc的語言裡可以搞。

但是,c++ 14 的 shared_ptr,atomic_load 和 atomic_store 提供了乙個解決辦法

#include #include #include #include #include #include #include using namespace std::chrono_literals;

class config

~config()

};std::shared_ptrglobalcfg;

std::shared_ptrreadcurrentcfg()

void reader()

void updateconfig(int id)

void writer()

}int main()

for (auto& thread : threads)

return 0;

}

執行結果

/home/xiaoju/.clion2016.2/system/cmake/generated/test2-90d0d4c6/90d0d4c6/debug/test2

[140460549330752] 0 created

[140460522039040] 0 accessed

[140460513646336] 0 accessed

[140460505253632] 0 accessed

[140460496860928] 0 accessed

[140460488468224] 0 accessed

[140460480075520] 0 accessed

[140460530431744] 1 created

[140460471682816] 1 accessed

[140460505253632] 0 accessed again

[140459993200384] 1 accessed

[140459984807680] 1 accessed

[140459976414976] 1 accessed

[140460522039040] 0 accessed again

[140460530431744] 2 created

[140459968022272] 2 accessed

[140459976414976] 1 accessed again

[140459959629568] 2 accessed

[140459951236864] 2 accessed

[140459942844160] 2 accessed

[140460530431744] 3 created

[140460488468224] 0 accessed again

[140459657656064] 3 accessed

[140459993200384] 1 accessed again

[140459649263360] 3 accessed

[140459984807680] 1 accessed again

[140459951236864] 2 accessed again

[140459640870656] 3 accessed

[140460471682816] 1 accessed again

[140460471682816] 1 deleted

[140459632477952] 3 accessed

[140459624085248] 3 accessed

[140459640870656] 3 accessed again

[140459615692544] 3 accessed

[140459607299840] 3 accessed

[140460480075520] 0 accessed again

[140459456329472] 3 accessed

[140460530431744] 4 created

[140459447936768] 4 accessed

[140459439544064] 4 accessed

[140459942844160] 2 accessed again

[140459968022272] 2 accessed again

[140459431151360] 4 accessed

[140459422758656] 4 accessed

[140459632477952] 3 accessed again

[140459414365952] 4 accessed

[140459405973248] 4 accessed

[140460530431744] 5 created

[140460496860928] 0 accessed again

[140459187894016] 5 accessed

[140460513646336] 0 accessed again

[140460513646336] 0 deleted

[140459439544064] 4 accessed again

[140459179501312] 5 accessed

[140460530431744] 6 created

[140459431151360] 4 accessed again

[140460530431744] 7 created

[140460530431744] 6 deleted

[140459959629568] 2 accessed again

[140459959629568] 2 deleted

[140459624085248] 3 accessed again

[140459414365952] 4 accessed again

[140459422758656] 4 accessed again

[140460530431744] 8 created

[140460530431744] 7 deleted

[140459456329472] 3 accessed again

[140459607299840] 3 accessed again

[140459657656064] 3 accessed again

[140459405973248] 4 accessed again

[140459649263360] 3 accessed again

[140459447936768] 4 accessed again

[140459447936768] 4 deleted

[140459179501312] 5 accessed again

[140460530431744] 9 created

[140460530431744] 8 deleted

[140460530431744] 10 created

[140460530431744] 9 deleted

[140459615692544] 3 accessed again

[140459615692544] 3 deleted

[140459187894016] 5 accessed again

[140459187894016] 5 deleted

[140460530431744] 11 created

[140460530431744] 10 deleted

[140460530431744] 12 created

[140460530431744] 11 deleted

[140460530431744] 13 created

[140460530431744] 12 deleted

[140460530431744] 14 created

[140460530431744] 13 deleted

[140460530431744] 15 created

[140460530431744] 14 deleted

[140460549330752] 15 deleted

process finished with exit code 0

可以看到15個物件全部都在最後一次訪問之後被釋放了。本地 cmake 的檔案

cmake_minimum_required(version 3.6)

project(test2)

set(cmake_cxx_flags "$ -std=c++1y -stdlib=libc++ -pthread")

set(source_files main.cpp)

add_executable(test2 $)

多執行緒環境下的shared ptr

多個執行緒讀寫同乙個shared ptr物件需要加鎖 注意分清shared ptr物件與shared ptr管理的物件 mutexlock mutex shared ptr globalptr void read void write 上面的read 和write 在臨界區之外都沒有再訪問globa...

linux使用shared ptr出現錯誤

我在不同的系統下採用下面的方式使用shared ptr以及vector std vector pointer arrays std vectorbject const iterator pointer iter 並採用如下初始化 const std shared ptrobject new obje...

C 共享指標shared ptr使用

共享指標 shared ptr 是現在的 boost 庫中提供的,並且應該是將來c 1x的標準庫中提供的乙個模板類。在此之前,iso iec 14882 2003 標準庫 中的 自動指標 auto ptr 也有類似的功能。顯然 shared ptr 要比 auto ptr 從功能上來說應該強大一些。...