As mentioned earlier, each thread has its own resources, but the code area is shared, that is, each thread can execute the same function. The problem this may cause is that several threads execute a function at the same time, causing data confusion and unpredictable results, so we must avoid this situation.
C# provides a keyword lock, which can define a section of code as a mutually exclusive section (critical section). The mutually exclusive section allows only one thread to enter execution at a time, while other threads must wait. In C#, the keyword lock is defined as follows:
lock(expression) statement_block
expression represents the object you wish to track, usually an object reference.
The statement_block is the code of the mutually exclusive section. This code can only be executed by one thread at a time.
The following is a typical example of using the lock keyword. The usage and purpose of the lock keyword are explained in the comments.
Examples are as follows:
Code
using System;
using System.Threading;
namespace ThreadSimple
{
internal class Account
{
int balance;
Random r = new Random();
internal Account(int initial)
{
balance = initial;
}
internal int Withdraw(int amount)
{
if (balance < 0)
{
//If balance is less than 0, throw an exception
throw new Exception("Negative Balance");
}
//The following code is guaranteed to complete before the current thread modifies the value of balance.
//No other thread will execute this code to modify the value of balance
//Therefore, the value of balance cannot be less than 0
lock(this)
{
Console.WriteLine("Current Thread:"+Thread.CurrentThread.Name);
//If there is no protection of the lock keyword, it may be after executing the if conditional judgment.
//Another thread executed balance=balance-amount and modified the value of balance.
//This modification is invisible to this thread, so it may cause the if condition to no longer hold.
//However, this thread continues to execute balance=balance-amount, so balance may be less than 0
if (balance >= amount)
{
Thread.Sleep(5);
balance = balance - amount;
return amount;
}
else
{
return 0; // transaction rejected
}
}
}
internal void DoTransactions()
{
for (int i = 0; i < 100; i++)
Withdraw(r.Next(-50, 100));
}
}
internal class Test
{
static internal Thread[] threads = new Thread[10];
public static void Main()
{
Account acc = new Account (0);
for (int i = 0; i < 10; i++)
{
Thread t = new Thread(new ThreadStart(acc.DoTransactions));
threads[i] = t;
}
for (int i = 0; i < 10; i++)
threads[i].Name=i.ToString();
for (int i = 0; i < 10; i++)
threads[i].Start();
Console.ReadLine();
}
}
}Monitor class locks an object
When multiple threads share an object, problems similar to common code will also occur. For this kind of problem, the lock keyword should not be used. A class Monitor in System.Threading needs to be used here. We can call it a monitor. , Monitor provides a solution for threads to share resources.
The Monitor class can lock an object, and a thread can operate on the object only if it obtains the lock. The object lock mechanism ensures that only one thread can access this object at a time in situations that may cause chaos. Monitor must be associated with a specific object, but because it is a static class, it cannot be used to define objects, and all its methods are static and cannot be referenced using objects. The following code illustrates the use of Monitor to lock an object:
...
Queue oQueue=new Queue();
...
Monitor.Enter(oQueue);
......//Now the oQueue object can only be manipulated by the current thread
Monitor.Exit(oQueue);//Release the lock
As shown above, when a thread calls the Monitor.Enter() method to lock an object, the object is owned by it. If other threads want to access this object, they can only wait for it to use the Monitor.Exit() method to release the lock. In order to ensure that the thread will eventually release the lock, you can write the Monitor.Exit() method in the finally code block in the try-catch-finally structure.
For any object locked by Monitor, some information related to it is stored in memory:
One is a reference to the thread that currently holds the lock;
The second is a preparation queue, which stores threads that are ready to acquire locks;
The third is a waiting queue, which holds a reference to the queue that is currently waiting for the object's status to change.
When the thread that owns the object lock is ready to release the lock, it uses the Monitor.Pulse() method to notify the first thread in the waiting queue, so the thread is transferred to the preparation queue. When the object lock is released, in the preparation queue The thread can immediately acquire the object lock.
The following is an example showing how to use the lock keyword and the Monitor class to implement thread synchronization and communication. It is also a typical producer and consumer problem.
In this routine, the producer thread and the consumer thread alternate. The producer writes a number, and the consumer immediately reads and displays it (the essence of the program is introduced in the comments).
The system namespaces used are as follows:
using System;
using System.Threading;
First, define a class Cell of the object being operated on. In this class, there are two methods: ReadFromCell() and WriteToCell. The consumer thread will call ReadFromCell() to read the contents of cellContents and display it, and the producer process will call the WriteToCell() method to write data to cellContents.
Examples are as follows:
Code
public class Cell
{
int cellContents; //Contents in the Cell object
bool readerFlag = false; // Status flag, when it is true, it can be read, when it is false, it is writing
public int ReadFromCell( )
{
lock(this) // What does the Lock keyword guarantee? Please read the previous introduction to lock.
{
if (!readerFlag)//If it is not readable now
{
try
{
//Wait for the Monitor.Pulse() method to be called in the WriteToCell method
Monitor.Wait(this);
}
catch (SynchronizationLockException e)
{
Console.WriteLine(e);
}
catch (ThreadInterruptedException e)
{
Console.WriteLine(e);
}
}
Console.WriteLine("Consume: {0}",cellContents);
readerFlag = false;
//Reset the readerFlag flag to indicate that the consumption behavior has been completed
Monitor.Pulse(this);
//Notify the WriteToCell() method (this method is executed in another thread and is waiting)
}
return cellContents;
}
public void WriteToCell(int n)
{
lock(this)
{
if(readerFlag)
{
try
{
Monitor.Wait(this);
}
catch (SynchronizationLockException e)
{
//When synchronized methods (referring to methods of the Monitor class except Enter) are called in asynchronous code areas
Console.WriteLine(e);
}
catch (ThreadInterruptedException e)
{
//Abort when the thread is in waiting state
Console.WriteLine(e);
}
}
cellContents = n;
Console.WriteLine("Produce: {0}",cellContents);
readerFlag = true;
Monitor.Pulse(this);
//Notify the waiting ReadFromCell() method in another thread
}
}
}
The producer class CellProd and the consumer class CellCons are defined below. They both have only one method ThreadRun(), so that the ThreadStart proxy object provided to the thread in the Main() function serves as the entrance to the thread.
public class CellProd
{
Cell cell; // Cell object being operated on
int quantity = 1; // The number of times the producer produces, initialized to 1
public CellProd(Cell box, int request)
{
//Constructor
cell = box;
quantity = request;
}
public void ThreadRun( )
{
for(int looper=1; looper<=quantity; looper++)
cell.WriteToCell(looper); //The producer writes information to the operation object
}
}
public class CellCons
{
Cell cell;
int quantity = 1;
public CellCons(Cell box, int request)
{
//Constructor
cell = box;
quantity = request;
}
public void ThreadRun( )
{
int valReturned;
for(int looper=1; looper<=quantity; looper++)
valReturned=cell.ReadFromCell();//Consumer reads information from the operation object
}
}
Then in the Main() function of the MonitorSample class below, what we have to do is to create two threads as producers and consumers, and use the CellProd.ThreadRun() method and CellCons.ThreadRun() method to perform operations on the same Cell object. operate.
Code
public class MonitorSample
{
public static void Main(String[] args)
{
int result = 0; //A flag bit. If it is 0, it means there is no error in the program. If it is 1, it means there is an error.
Cell cell = new Cell( );
//The following uses cell to initialize the CellProd and CellCons classes. The production and consumption times are both 20 times.
CellProd prod = new CellProd(cell, 20);
CellCons cons = new CellCons(cell, 20);
Thread producer = new Thread(new ThreadStart(prod.ThreadRun));
Thread consumer = new Thread(new ThreadStart(cons.ThreadRun));
//Both the producer thread and the consumer thread have been created, but execution has not started.
try
{
producer.Start( );
consumer.Start( );
producer.Join( );
consumer.Join( );
Console.ReadLine();
}
catch (ThreadStateException e)
{
//When the thread cannot perform the requested operation due to its state
Console.WriteLine(e);
result = 1;
}
catch (ThreadInterruptedException e)
{
//Abort when the thread is in waiting state
Console.WriteLine(e);
result = 1;
}
//Although the Main() function does not return a value, the following statement can return the execution result to the parent process
Environment.ExitCode = result;
}
}
In the above routine, synchronization is done by waiting for Monitor.Pulse(). First, the producer produces a value, and at the same time the consumer is in a waiting state until it receives a "Pulse" from the producer to notify it that the production has been completed. After that, the consumer enters the consuming state, and the producer begins to wait for the consumer to complete. The "pulse" emitted by Monitor.Pulese() will be called after the operation.
Its execution result is simple:
Produce: 1
Consume: 1
Produce: 2
Consume: 2
Produce: 3
Consume: 3
...
...
Produce: 20
Consume: 20
In fact, this simple example has helped us solve big problems that may arise in multi-threaded applications. As long as you understand the basic methods of resolving conflicts between threads, it is easy to apply it to more complex programs.
-