It is often encountered that we need to operate a certain data or read and write a certain file at the same time. We often cannot handle these operations well in the past. Since the keyword lock was introduced in the C# language, the above problems have been easier to solve. The following is a simple piece of code.
public class AccessControl()
{
private static object privateObjectLock = new object();
public static AccessResult()
{
lock(privateObjectLock)
{
//Data operation statement
}
}
}
=====================================================
Usage of lock in C# multithreading
When working as a mailbox receiving gateway, we encountered the following requirements. We required that a receiving thread be opened for each mailbox, collect it from the POP3 server, and then store the emails on a unified FTP server, and require that the emails be charged from 1 according to the receiving and receiving and charging.
The method I implemented is to new instances for each email address, and then assign POP3 email address, username, password and other parameters respectively. This involves a problem of number synchronization, because each thread receiving the email executes itself, so the action of obtaining the number and incrementing the message is mutually exclusive.
The number is represented by a static variable as follows
class EmailInfo
{
public static int CurrentNumber;
}
Then, in the current thread, this step is
_CurrentNumber=++;
Although this is a sentence, it is divided into multiple steps when the computer is running, as follows
Add the return value to _CurrentNumber
, Maybe thread 1 performed the operation of adding 1, but has not yet obtained the return value. At this time, thread 2 performed the operation of adding 1, and then thread 1, and thread 2 obtained the return value the same, which loses the effect of incrementing in order.
At this time, I found the method about thread synchronization on the Internet. In fact, I just need to use the lock statement to lock the incrementing segment. The relevant usage introduced is
lock(this)
{
_CurrentNumber=++;
}
I thought this would be successful, but who knew it would be invalid. After searching repeatedly, I found that I didn’t figure out the usage of lock. Because the information mentioned online is relatively simple, it is to directly new an object and then create multiple threads to run it for a function of the object, so its synchronization is as long as it locks this, that is, it itself. Because there is only one instance in operation at this time, and I new to create multiple objects, locking each of my own instances, so of course it is invalid.
So naturally I thought of a solution and just lock the same example. Because the parameters of each email receiving thread are different, I will still create several real images new, but the lock method is changed to the following
First add a static variable to EmailInfo
class EmailInfo
{
public static object syncRoot = new object();
public static int CurrentNumber;
}
Then lock is changed to
lock()
{
_CurrentNumber=++;
}
You can achieve the desired effect.
After writing it out, I found that it was a very simple thing.Just lock a (static static) thing that everyone visits together., but this has been a long time since I was involved. The reason is that I do not have a serious understanding of the information I am looking for, and I use it as soon as I use it. I understand everything, but it is not the case.
I think some beginners may make similar mistakes, so I wrote them out to encourage each other. Welcome friends who study C# or use C# at work to communicate with me
Copy the codeThe code is as follows:
public class AccessControl()
{
private static object privateObjectLock = new object();
public static AccessResult()
{
lock(privateObjectLock)
{
//Data operation statement
}
}
}
=====================================================
Usage of lock in C# multithreading
When working as a mailbox receiving gateway, we encountered the following requirements. We required that a receiving thread be opened for each mailbox, collect it from the POP3 server, and then store the emails on a unified FTP server, and require that the emails be charged from 1 according to the receiving and receiving and charging.
The method I implemented is to new instances for each email address, and then assign POP3 email address, username, password and other parameters respectively. This involves a problem of number synchronization, because each thread receiving the email executes itself, so the action of obtaining the number and incrementing the message is mutually exclusive.
The number is represented by a static variable as follows
Copy the codeThe code is as follows:
class EmailInfo
{
public static int CurrentNumber;
}
Then, in the current thread, this step is
Copy the codeThe code is as follows:
_CurrentNumber=++;
Although this is a sentence, it is divided into multiple steps when the computer is running, as follows
Add the return value to _CurrentNumber
, Maybe thread 1 performed the operation of adding 1, but has not yet obtained the return value. At this time, thread 2 performed the operation of adding 1, and then thread 1, and thread 2 obtained the return value the same, which loses the effect of incrementing in order.
At this time, I found the method about thread synchronization on the Internet. In fact, I just need to use the lock statement to lock the incrementing segment. The relevant usage introduced is
Copy the codeThe code is as follows:
lock(this)
{
_CurrentNumber=++;
}
I thought this would be successful, but who knew it would be invalid. After searching repeatedly, I found that I didn’t figure out the usage of lock. Because the information mentioned online is relatively simple, it is to directly new an object and then create multiple threads to run it for a function of the object, so its synchronization is as long as it locks this, that is, it itself. Because there is only one instance in operation at this time, and I new to create multiple objects, locking each of my own instances, so of course it is invalid.
So naturally I thought of a solution and just lock the same example. Because the parameters of each email receiving thread are different, I will still create several real images new, but the lock method is changed to the following
First add a static variable to EmailInfo
Copy the codeThe code is as follows:
class EmailInfo
{
public static object syncRoot = new object();
public static int CurrentNumber;
}
Then lock is changed to
Copy the codeThe code is as follows:
lock()
{
_CurrentNumber=++;
}
You can achieve the desired effect.
After writing it out, I found that it was a very simple thing.Just lock a (static static) thing that everyone visits together., but this has been a long time since I was involved. The reason is that I do not have a serious understanding of the information I am looking for, and I use it as soon as I use it. I understand everything, but it is not the case.
I think some beginners may make similar mistakes, so I wrote them out to encourage each other. Welcome friends who study C# or use C# at work to communicate with me