If you want to have fun with threads, you should share mutable data between them. In order to get no data race and, therefore, undefined behavior, you have to think about the synchronization of your threads.
C++11 is the first C++ standard that deals with concurrency. The basic building block for concurrency is a thread; therefore, most of the rules are explicitly about threads. This changed dramatically with C++17.
What is the fastest way to add the elements of a std::vector?. A question which I will pursue in the next posts. I use the single-threaded addition as the reference number. In further posts, I discuss atomics, locks, and thread-local data.
There are a lot of issues with the singleton pattern. I'm totally aware of that. But the singleton pattern is an ideal use case for a variable, which has only to be initialized in a thread-safe way. From that point on you can use it without synchronization. So in this post, I discuss different ways to initialize a singleton in a multithreading environment. You get the performance numbers and can reason about your uses cases for the thread-safe initialization of a variable.
The easiest way to solve the undefined behaviour in the post Ongoing Optimization: Unsynchronized access is, to use a lock.
With C++14 came reader-writer locks. The idea is straightforward and promising. Arbitrary reading threads can access the critical region at the same time, but only one thread is allowed to write.
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.
Currently are 147 guests and no members online
Kubik-Rubik Joomla! Extensions