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 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.