As we already know, the data in an actual system is often represented in the form of a data structure, and when it comes to concurrent operations on a data structure, performance is a big deal. In Chapter 3, Language-Level Concurrency and Parallelism in C++, we learned how to write a thread-safe stack. However, we used locks and condition variables to implement it. To explain how to write a lock-free data structure, let's write a very basic queue system using a producer/consumer paradigm without using locks or condition variables. This will improve the performance of the code for sure. Rather than using a wrapper over a standard data type, we will roll it out from scratch. We have made an assumption that there is a single producer and a single consumer in this case:
template<typename T> class Lock_free_Queue { private: struct Node { std::shared_ptr<T> my_data; Node* my_next_node; Node() : my_next_node(nullptr...