Category : concurrency

Following is a simplified implementation of a semi-lock-free spsc queue.I intentionally simplified it to be easier to read and easier to reproduce the deadlock problem. The problem is in the memory-ordering of write_pos_ and read_pos_ with respect to enqueuer_in_sleep_ and dequeuer_in_sleep_ which can lead to deadlock in certain circumstances that I commented in the code. ..

Read more

In our program, we have a class FooLogger which logs specific events (strings). We use the FooLogger as a unique_ptr. We have two threads which use this unique_ptr instance: Thread 1 logs the latest event to file in a while loop, first checking if the instance is not nullptr Thread 2 deallocates the FooLogger unique_ptr ..

Read more

Below is the example source code of thread safe list using lock from c++ concurrency in action 2nd. template<typename T> class threadsafe_list { struct node { std::mutex m; std::shared_ptr<T> data; std::unique_ptr<node> next; node(): next() {} node(T const& value): data(std::make_shared<T>(value)) {} }; node head; public: threadsafe_list() {} ~threadsafe_list() { remove_if([](node const&){return true;}); // (1) why remove ..

Read more

I have been attempting to write a simple program to experiment with vectors of threads. I am trying to create a thread at the moment, but I am finding that I am running into an error that my constructor is not initializing properly, with the error that there is no matching constructor for std::thread matching ..

Read more

Below is the example source code of thread safe list using lock from c++ concurrency in action 2nd. template<typename T> class threadsafe_list { struct node { std::mutex m; std::shared_ptr<T> data; std::unique_ptr<node> next; node(): next() {} node(T const& value): data(std::make_shared<T>(value)) {} }; node head; public: threadsafe_list() {} ~threadsafe_list() { remove_if([](node const&){return true;}); // (1) (2) } ..

Read more