Modernes C++
This page is the starting point for my blog Modernes C++. A simple overview of my existing and upcoming posts.
This author has not written his bio yet.
But we are proud to say that Rainer Grimm contributed 691 entries already.
This page is the starting point for my blog Modernes C++. A simple overview of my existing and upcoming posts.
std::packaged_task enables you to write a simple wrapper for a callable, which you can invoke later.
std:.async feels like an asynchronous function call. Under the hood std::async is a task. One, which is extremely easy to use.
Tasks were one of the latest additions to the C++11 standard. They give you a better abstraction than threads. In the general case, they should be your first choice.
Condition variables allow us to synchronize threads via notifications. So, you can implement workflows like sender/receiver or producer/consumer. In such a workflow, the receiver waits for the sender’s notification. If the receiver gets the notification, it continues its work.
By using the keyword thread_local, you define the thread local data. Thread-local can easily be explained in a few words.
The story is simple if the data is not modified when shared between threads. The data has only to be initialized in the thread-safe way. It is not necessary to use an expensive lock for each access.
If the previous post showed something, it’s that you should use mutexes with great care. That’s why you should wrap them in a lock.
The usage of mutexes seems extremely simple. There is a critical section in the code that a single thread can only access at any point in time. It’s ensured by a mutex m. The calls m.lock() and m.unlock() guarantee this exclusivity. But the devil is in the details.
With C++14 came reader-writer locks. The idea is straightforward and promising. Arbitrary reading threads can access the critical region simultaneously, but only one thread is allowed to write.