. NET brief introduction to component programming (context and synchronization domain)

Source: Internet
Author: User

We continue to learn about. NET multithreading technology. The content of this article may be a bit complicated. After breaking common sense, changing a new thinking model is the biggest headache. This article involves some uncommon concepts, such as context and synchronization domain. I have recently come into contact with these advanced technologies on component programming. If you study together, there are time constraints for the greatest difficulties. As long as we stick to them.

In my previous article ". in a brief introduction to component program design (multithreading and concurrency management I) ", I just initially led us to learn some basic principles about multithreading, including thread switching, the thread starts, executes, waits, and ends.

This article focuses on learning about thread synchronization and mutex mechanisms. In a multi-threaded application, at least one main thread is running. To increase the application throughput, we must use the multi-thread principle. [Wang qingpei has all rights reserved. For more information, please sign it.]

. NET context (ContextBoundObject object)

What is context? Do not confuse it with the context in ASP. NET. This context is an adjective and has different meanings in different occasions. In ASP. in. NET, Context refers to the Context object. This object basically contains information about the entire lifecycle of the HTTP protocol and obtains some basic information about the client browser, you can also obtain information about the HTTP protocol.

The context here is. the minimum logical range of NET program execution, ASP. NET context is viewed from the perspective of B/S programming model, and the context here is. from the perspective of NET underlying running, the latter is the context of the code, and the former is the context of the entire lifecycle.

I always thought before I got in touch with ContextBoundObject. the minimum logical scope of the execution of the NET program is the application domain (AppDomain). Once the application domain is known, the context is used to determine the logical attribution of the object), Transaction processing (Transaction), Enterprise Services (Enterprise) and other aspects need to use context to plan objects. The following shows how to use context to synchronize threads.

Figure 1:

650) this. width = 650; "alt =" "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/1S414K24-0.png "/>

. NET Synchronization domain (Synchronization feature)

The concept of a synchronous domain comes from multithreading. When we perform multi-threaded operations, when many threads access a single memory object at the same time, A lock must be used to ensure that only one thread enters the object for operation. The concept of a synchronization domain is to synchronize a region rather than a single object.

A thread is the execution path of the Code. As long as the execution path belongs to the thread range, how can we separate another synchronization region from the execution path. [Wang qingpei has all rights reserved. For more information, please sign it.]

A critical resource is a description in the system that can only be accessed by one thread at a time. Let's assume that we are a thread. If we want to get something at home, the door will be the synchronization lock of the thread. When we go in, we will lock the door from the inside and open the door when we go out, to make it easier for your family to come in. This house is a critical resource. This description may be unreliable!

Figure 2:

650) this. width = 650; "alt =" "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/1S4146114-1.png "/>

Use context and synchronization domains for Thread Synchronization

Next we will combine the context and synchronization domain principles for thread synchronization. See the following code:

 
 
  1. Using System;
  2. Using System. Collections. Generic;
  3. Using System. Text;
  4. Using System. Threading;
  5. Using System. Runtime. Remoting;
  6. Using System. Runtime. Remoting. Contexts;
  7.  
  8. Namespace ConsoleApplication1. multithreading and concurrency Management
  9. {
  10. // If the context is not added, the thread lock area is the object. If the context is added, the lock area is the logical context.
  11. [Synchronization (SynchronizationAttribute. REQUIRED, true)] // re-enter the Synchronization domain
  12. Public class MyClass: ContextBoundObject
  13. {
  14. Public void DoWork ()
  15. {
  16.  
  17. Int I = 0;
  18. While (true)
  19. {
  20. Console. WriteLine (Thread. CurrentThread. ManagedThreadId + "|" + I ++ );
  21. If (I = 10)
  22. {
  23. Console. WriteLine ("---------------------------------------------");
  24. Console. Read ();
  25. Break;
  26. }
  27. }
  28.  
  29. }
  30. }
  31. }

In this Code, I added the Synchronization feature to the Myclass class and inherited from the context object ContextBoundObject. The two must be used together, and the synchronization domain is only valid in the context.

MSDN: Applying SynchronizationAttribute to a context-bound object will lead to the creation of wait handles and automatic resetting events, which may not be recycled as garbage. Therefore, do not create a large number of context binding objects marked with SynchronizationAttribute in a short period of time .]

Figure 3:

650) this. width = 650; "alt =" "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/1S41423D-2.png "/>

Figure 4:

650) this. width = 650; "alt =" "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/1S4143364-3.png "/>

Let's look at the call code:

 
 
  1. Thread currentthread = Thread.CurrentThread;  
  2.             Console.WriteLine(currentthread.Name + currentthread.ManagedThreadId);  
  3.  
  4.             MyClass myclass = new MyClass();  
  5.  
  6.             Thread thread = new Thread(new ThreadStart(myclass.DoWork));  
  7.  
  8.  
  9.             Thread thread2 = new Thread(new ThreadStart(myclass.DoWork));  
  10.             thread2.Start();  
  11.             thread.Start();  
  12.  
  13.             thread2.Join();  
  14.             thread.Join();  
  15.             Console.WriteLine(currentthread.Name + currentthread.ManagedThreadId);  
  16.             Console.Read(); 

Figure 5:

650) this. width = 650; "alt =" "border =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/1S41453O-4.png "/>

For convenience, set the number of loops to a smaller value. If you want to test it, set the number to a larger value. [Wang qingpei has all rights reserved. For more information, please sign it.]

There are several enumeration values in the SynchronizationAttribute object to determine whether to share a synchronization domain. If you are interested, you can try it yourself. I will not talk about it here.

Summary: The synchronization domain and context object are good methods for automatic thread synchronization, but they are too large to lock, which will inevitably lead to a decline in the system throughput, so in the following articles, we will learn how to use manual synchronization to implement more flexible synchronization (such as Monitor and WaitHandler) and lock at a very small granularity.

This article is from the pattern driven the world blog, please be sure to keep this source http://wangqingpei557.blog.51cto.com/1009349/652057

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.