Category : thread-safety

I’m trying to create a "guarded_thread", but I receive an error ‘operator=’ is a private member of ‘std::__1::thread’. Here is my code: struct guarded_thread : std::thread{ using std::thread::thread; using std::thread::operator=; ~guarded_thread(){if(joinable())join();} }; A function did the work, but I want to know how to create it the other way void Guarded_thread(std::thread &Thread){ if (Thread.joinable()) { ..

Read more

Source has following comment: // Return the number of items in the queue; thread unsafe std::ptrdiff_t size() const { return my_queue_representation->size(); } But then I looked at internal impl, and it all seems to be thread safe(operations are loads of std::atomic and then some substraction). std::ptrdiff_t size() const { __TBB_ASSERT(sizeof(std::ptrdiff_t) <= sizeof(size_type), NULL); std::ptrdiff_t hc ..

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

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