//******************************************************** // The following code example is taken from the book // C++20 - The Complete Guide // by Nicolai M. Josuttis (www.josuttis.com) // http://www.cppstd20.com // // The code is licensed under a // Creative Commons Attribution 4.0 International License // http://creativecommons.org/licenses/by/4.0/ //******************************************************** #include #include #include // includes now using namespace std::literals; // for duration literals template class AtomicList { private: struct Node { T val; std::shared_ptr next; }; std::atomic> head; public: AtomicList() = default; void insert(T v) { auto p = std::make_shared(); p->val = v; p->next = head; while (!head.compare_exchange_weak(p->next, p)) { // atomic update } } void print() const { std::cout << "HEAD"; for (auto p = head.load(); p; p = p->next) { // atomic read std::cout << "->" << p->val; } std::cout << std::endl; } }; int main() { AtomicList alist; // populate list with elements from 10 threads: { std::vector threads; for (int i = 0; i < 100; ++i) { threads.push_back(std::jthread{[&, i]{ for (auto s : {"hi", "hey", "ho", "last"}) { alist.insert(std::to_string(i) + s); std::this_thread::sleep_for(5ns); } }}); } } // wait for all threads to finish alist.print(); // print resulting list }