Synchronization

Starting and manipulating a Thread is the easy part, made easier with the classes and facilities that the CLR provides. The hard part is synchronizing access to and manipulation of shared data.

When you are accessing or manipulating shared data, critical sections of code often have to happen atomically or not at all. In Listing 11.12, the critical section is the loop in the Thread entry point. You want all of the data generated in the loop to occur at the same time, uninterrupted. This could just as easily have been links in a linked list or shared variables being swapped. It is easier to show the interruption with a simple loop. To make sure that the OS will preempt the Thread, a Fibonacci number will be computed to simulate ...

Get .NET Common Language Runtime Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.