. NET Framework considerations-thread synchronization-method level

Source: Internet
Author: User

It is about how the framework is considerate to programmers, not C #.

 

This is a subtle effort of the framework on concurrency:

   using System.Runtime.CompilerServices;


Public class myclass
{
[Methodimpl (methodimploptions. Synchronized)]// Considerate?
Public void dosomething ()
{
/* Method code */
}
// Class members
}

This Code uses [methodimpl (methodimploptions. Synchronized)] to implement multi-thread synchronization at the method level.

 

The above code is logically equivalent to the following:

Public class myclass
{
Public void dosomething ()
{
Lock (this) // refer to C # considerations-Lock
{
/* Method code */
}
}
// Class members
}

 

 

What about the two routines? For machines, I guess this is not a problem at all. If I like the fans of AOP, I would naturally think that the first solution is even cooler. The strange thing is that I haven't seen anyone using the first routine in my project, and I use locks without exception. I have always wondered why the result is so one-sided? Does lock look cooler than [methodimpl (methodimploptions. Synchronized?

 

(Press: code source from <programming. NET components> 2nd. Edition Juval Lowy)

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.