Locking and Synchronization

When threads compete with each other, each thread tries to get enough hardware resources. This can easily lead to troubles, so it's necessary to find solutions for this problem. In this section, we have a look at the most basic algorithms for working with threads safely. Because threading is a complex subject, we deal with only the very basics. However, for most applications, we provide enough information.

Locking

Locking is an interesting yet complex issue. Let's get started with an example:

 using System; using System.Threading; public class Demo { public int data = 0; public static void Main() { Demo MyDemo = new Demo(); Thread th1 = new Thread( new ThreadStart(MyDemo.CalcAndReport)); th1.Name = "Thread 1"; Thread ...

Get Mono Kick Start now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.