C # multi-thread lock usage

Source: Internet
Author: User
I recently read some C # code and found that many of them have not been used before. I can only check them online and sort them out. We often encounter the need to operate on a certain data at the same time, or perform read and write operations on a certain file. We often cannot process these operations well before, since the lock keyword is introduced in the C # language, the above problem is easy to solve. Below is a simple piece of code.

Public class accesscontrol ()
{
Private Static object privateobjectlock = new object ();

Public static accessresult ()
{
Lock (privateobjectlock)
{
// Data operation statement www.elvin.com
}
}
}

========================================================== ==============================================

C # multi-thread lock usage 

The mailbox receiving gateway meets the following requirements: Enable a receiving thread for each mailbox, collect the mail from the POP3 server, and store the mail on the same FTP server, the number of emails starting from 1 is required.
My implementation method is to create an instance for each mailbox, and then assign the POP3 email address, user name, password, and other parameters to each mailbox. This involves a number synchronization problem, because each thread that receives the mail executes the number itself, so the operation to get the number and increase the number is mutually exclusive.
The following code is represented as a static variable:
Class emailinfo
{
Public static int currentnumber;
}
In the current thread, this step is
_ Currentnumber = ++ emailinfo. currentnumber;
Although this is a sentence, it can be divided into multiple steps during computer operation, as shown below:
Emialinfo. currentnumber plus 1 -- emailinfo. currentnumber returned value to _ currentnumber
, Maybe thread 1 executes emailinfo. currentnumber is added to 1, but the return value has not been obtained. At this time, thread 2 executes emailinfo. the currentnumber plus 1 operation, and then thread 1, thread 2 gets the return value is the same, thus losing the incremental order.
At this time, I found the online thread synchronization method. In fact, I can use the lock statement to lock the incremental segment. The related usage is as follows:
Lock (this)
{
_ Currentnumber = ++ emailinfo. currentnumber;
}
In this way, we thought we could succeed. Who knows whether it is still invalid? We found that we didn't figure out the lock usage after repeated searches. Because of the materials on the Internet, the example is relatively simple. It is to directly create a new object, and then create multiple threads for a function of the object, so it only needs to lock this for its synchronization. At this time, only one instance is running, and I am new with multiple objects, locking each of its own instances is of course ineffective.
So naturally, if you think of a solution, you just need to lock the same instance. Because the parameters of each mail receiving thread are different, several real images are generated for new, but the lock method is changed to the following:
Add a static variable to emailinfo first
Class emailinfo
{
Public static object syncroot = new object ();
Public static int currentnumber;
}
Then change lock
Lock (emailinfo. syncroot)
{
_ Currentnumber = ++ emailinfo. currentnumber;
}
You can achieve the desired effect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.