This is my first multi-threaded implementation, so it’s probably a beginners mistake. The threads handle the rendering of every second row of pixels (so all rendering is handled within each thread). The problem persists if the threads render the upper and lower parts of the screen respectively.
Both threads read from the same variables, can this cause any problems? From what I’ve understood only writing can cause concurrency problems…
Can calling the same functions cause any concurrency problems? And again, from what I’ve understood this shouldn’t be a problem…
The only time both threads write to the same variable is when saving the calculated pixel color. This is stored in an array, but they never write to the same indices in that array. Can this cause a problem?
Multi-threaded rendered image
(Spam prevention stops me from posting images directly..)
Ps. I use the exactly same implementation in both cases, the ONLY difference is a single vs. two threads created for the rendering.
This should be ok. Obviously, as long the data is initialized before the two threads start reading and destroyed after both threads have finished.
Yes and no. Too hard to tell without the code. What does the function do? Does it rely on shared state (e.g.
staticvariables, global variables, singletons…)? If yes, then this is definitely a problem. If there is never any shared state, then you’re ok.Maybe sometimes. An array of what? It’s probably safe if
sizeof(element) == sizeof(void*), but the C++ standard is mute on multithreading, so it doesn’t force your compiler to force your hardware to make this safe. It’s possible that your platform could be biting you here (e.g. 64bit machine and one thread writing 32bits which might overwrite an adjacent 32bit value), but this isn’t an uncommon pattern. Usually you’re better off using synchronization to be sure.You can solve this in a couple of ways:
The lack of commitment in my answers are what make multi-threaded programming hard 😛
For example, from Intel® 64 and IA-32 Architectures Software Developer’s Manuals, describes how different platforms gaurantee different levels of atomicity: