About This eBook by Bart De Smet

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Atomicity (or Lack Thereof) Illustrated

To give a programming-related example of the need for synchronization, let’s keep things as simple as we can and take a look at a shared counter. None of the following code should be hard to understand at this point. We simply create and start a new thread to increment the counter repeatedly. In the meantime, the main thread performs its duties, decrementing the same counter value. Finally, we join the incrementing thread (called up) with the main thread and print the result.

static void Main(){    int n = 0;    var up = new Thread(() =>    {        for (int i = 0; i < 1000000; i++)            n++;    });    up.Start();    for (int i = 0; i < 1000000; i++)        n--;    up.Join(); ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required