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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access