Martin Drozdik
Martin Drozdik

Reputation: 13303

Are there some race conditions that can happen only with true concurrency?

If you have a computer with a single core, you can easily get into all sorts of trouble with threaded code. For example you have:

class A 
{
    int always_even = 0;

public:
    void doSomething()
    {
        always_even++; 
        always_even++;
    }       
}

then thread one can get past the first increment and then be swapped out and a different thread would see that A is violating an invariant.

However, I am intrigued by the question, are there some race conditions or concurrency bugs that manifest themselves only on architectures with true parralelism, i.e. with two or more threads executing at the same physical time?

Upvotes: 1

Views: 57

Answers (1)

Alexander Kogtenkov
Alexander Kogtenkov

Reputation: 5780

The keywords to look for are memory barrier and memory model (or, a more general term - consistency models). In short, when threads are switched on a single core, every thread sees the same order of reads and writes. If there are different cores/CPUs with different cashes, the order of reads and writes may look differently for each of them. As soon as an algorithm depends on the memory access order, the program using it can exhibit failures on multi-core/multi-CPU machine, but never fail on a single-core one.

Upvotes: 3

Related Questions