Recently looking at some C # code, found that many are not previously contacted, can only search the Internet, the better sorting down.
Often encountered at the same time need to operate on a certain data, or read and write to a file, for these operations we often do not very good processing, since the C # language introduced lock this keyword, the above problem is more easily resolved, the following is a simple code.
public class AccessControl()
{
private static object privateObjectLock = new object();
public static AccessResult()
{
lock(privateObjectLock)
{
//数据操作语句
}
}
}
In the mailbox to receive the gateway when encountered the following requirements, the request for each mailbox to open a receiving thread, from the POP3 server collection, and then the message to the unified FTP server, request mail according to receive shun charge starting from 1 to fill number.
I implemented the method for each mailbox new instance, and then assigned to the POP3 email address, username, password and other parameters. This involves a problem with number synchronization because each thread that receives the message executes itself, so getting the number and incrementing the action is mutually exclusive.
The number is represented as a static variable with the following
class EmailInfo
{
public static int CurrentNumber;
}
The current thread gets this step to
_currentnumber=++emailinfo.currentnumber;
Although this is a sentence, but when the computer runs is divided into several steps, as follows
Emialinfo.currentnumber plus 1--emailinfo.currentnumber return value to _currentnumber
, perhaps thread 1 performed the Emailinfo.currentnumber plus 1 operation, but did not get the return value. Thread 2 then executes Emailinfo.currentnumber plus 1, then threads 1, thread 2 Gets the same return value, which loses the incremental effect in order.
At this point to find the Internet about the method of thread synchronization, in fact, lock statement with the increase in the section can be. And the related usage of the introduction is
lock(this)
{
_CurrentNumber=++EmailInfo.CurrentNumber;
}
The thought that this can be successful, who knows or ineffective, repeated search only found that did not understand the use of lock. Because the information on the Internet, the example is relatively simple, is directly new to a image, and then for the image of a function created a number of threads to run, so its synchronization as long as lock this is the only way it itself. Because there is only one instance at this time, and I am new out of multiple pairs, lock every instance of myself so of course it is invalid.
So naturally think of a solution, lock the same instance on the line.
Because I have a different parameter for each message receiving thread, I still have a few real image, but the lock method is changed as follows
First add a static variable to the EmailInfo
class EmailInfo
{
public static object syncRoot = new object();
public static int CurrentNumber;
}
然后lock改为
lock(EmailInfo.syncRoot)
{
_CurrentNumber=++EmailInfo.CurrentNumber;
}
You can achieve the desired effect.