Threads imply code running simultaneously on multiple processor cores in a computer. The processors and the code running within them have access to the entire memory of the computer. This means that code in two threads can attempt to change the same piece of data in memory at the same time. As you can imagine, that would not be a good idea, and many different strategies have been created over the years to deal with this situation in multithreaded code.
Thread safety and synchronization primitives
Multithreaded Monte Carlo simulation
Remember the code we saw in Chapter 7, Accelerating Code with the GPU, to calculate the value of pi using Monte Carlo simulation. It used a loop to calculate a position based on draws from a random...