[Progressive] design a thread synchronization component that simulates parallel operations

Source: Internet
Author: User

Do you have the following scenarios:

    1. Some steps in an algorithm are expected to be executed in parallel to improve computing efficiency.
    2. In a workflow, after an activity is executed, multiple concurrent Branch activities are displayed. After all branch activities are processed, the system returns to the main process to continue the next activity.

This article will design a component to meet the requirements of the above scenarios to support your design.

We can analyze the requirements from the above scenarios:

"When the main thread is executed halfway, it needs to wait for several sub-threads to finish processing and continue the execution of the main thread ."

Therefore, we will design a component based on this feature. You may have thought of a simple use of event and other notification mechanisms to let the sub-thread send a notification after execution and then perform the next operation:

Describes the execution model formed by using events, messages, and other mechanisms.

Let's take a look at this figure:

Do you want to allow the primary/subthread programming to maintain linear syntax writing in a method area, rather than disrupting the original writing sequence and coherence through callback or other methods?

 

View parallel

Dotnet3.5 began to provide support for Parallel Computing. Plinq is the parallel version of LINQ. theading space (a separate parallel library system. theading. DLL), 4.0 greatly enhances the support for parallel programming. Plinq provides us with the following parallel-enabled syntax to traverse the set:

  1. New List <Object> (). asparallel (). forall (O => {});

 

Test:

  1. VaR P = new list <action> () {() => {thread. Sleep (1000); console. writeline ("subtask 1 completed ");}
  2. , () => {Thread. Sleep (1000); console. writeline ("subtask 2 completed ");}
  3. , () => {Thread. Sleep (1000); console. writeline ("subtask 3 completed ");}
  4. , () => {Thread. Sleep (1000); console. writeline ("subtask 4 completed ");}
  5. , () => {Thread. Sleep (1000); console. writeline ("subtask 5 completed ");}
  6. , () => {Thread. Sleep (1000); console. writeline ("subtask 6 completed ");}
  7. , () => {Thread. Sleep (1000); console. writeline ("subtask 7 completed") ;}}. asparallel ();
  8. Datetime begin = datetime. now;
  9. P. forall (O => O ());
  10. Console. writeline ("? "+ (Datetime. Now-begin). totalmilliseconds );
  11. Begin = datetime. now;
  12. P. forall (O => O ());
  13. Console. writeline ("? "+ (Datetime. Now-begin). totalmilliseconds );

The execution efficiency of the above Code can be increased by almost the same multiples based on the number of CPU cores. In fact, the 3.5 parallel support is only a preliminary preview. Improper design and use may not lead to much improvement. The implementation principle is also the use of multithreading, and the system. theading provides a dedicated thread pool to optimize the use of threads.

Back to our topic, the requirements set above are somewhat similar to parallel processing. This is similar in execution behavior, but the design goals are still different. We aim to design thread synchronization components, to support the synchronization of the main and sub-threads and the smoothness of the syntax. The purpose of parallel computing is to make full use of the computing power of the CPU.

Since it is to thread synchronization, there must be a corresponding synchronization mechanism, here using waithandle (can refer to the http://msdn.microsoft.com/zh-cn/library/system.threading.waithandle (vs.80). aspx) to complete this design. Both autoresetevent and manualresetevent are derived from waithandle. Similar to the signal lamp, the former automatically wakes up a thread, and the latter can be programmed to wake up multiple threads.

Code downloading will be included later in the article, and there is still room for improvement in the design, such as the use of threads.

Call Syntax:

  1. // Create
  2. VaR paralle = paralle. Create ();
  3. // Add task
  4. Paralle. Add () =>{ thread. Sleep (1000 );});
  5. // Start
  6. Paralle. Begin ();
  7. // Return main thread
  8. //...

Test code:

  1. Console. writeline ("And? Simulated component testing ?? ");
  2. Console. writeline ("head service starts. ");
  3. Thread. Sleep (5000 );
  4. Console. writeline ("the director is suspended. ");
  5. Console. writeline ("");
  6.  
  7. // Used for the first time
  8. Datetime begin = datetime. now;
  9.  
  10. VaR P = paralle. Create ();
  11. P. Add () => {thread. Sleep (1000); console. writeline ("subtask 1 completed ");}
  12. , () => {Thread. Sleep (1000); console. writeline ("subtask 2 completed ");}
  13. , () => {Thread. Sleep (1000); console. writeline ("subtask 3 completed ");}
  14. , () => {Thread. Sleep (1000); console. writeline ("subtask 4 completed ");});
  15.  
  16. Console. writeline ("initialization? When (MS )? "+ (Datetime. Now-begin). totalmilliseconds );
  17. Begin = datetime. now;
  18.  
  19. P. Begin ();
  20.  
  21. Console. writeline ("executive ?? When (MS )? "+ (Datetime. Now-begin). totalmilliseconds );
  22.  
  23. Console. writeline ("");
  24. Console. writeline ("continue. ");
  25. Thread. Sleep (2000 );
  26. Console. writeline ("the director is suspended. ");
  27. Console. writeline ("");
  28.  
  29. // Second use
  30. P. Clear ();
  31.  
  32. Begin = datetime. now;
  33.  
  34. P. Add () => {thread. Sleep (1000); console. writeline ("subtask 5 completed ");}
  35. , () => {Thread. Sleep (1000); console. writeline ("subtask 6 completed ");}
  36. , () => {Thread. Sleep (1000); console. writeline ("subtask 7 completed ");}
  37. , () => {Thread. Sleep (1000); console. writeline ("subtask 8 completed ");});
  38.  
  39. Console. writeline ("initialization? When (MS )? "+ (Datetime. Now-begin). totalmilliseconds );
  40. Begin = datetime. now;
  41.  
  42. P. Begin ();
  43.  
  44. Console. writeline ("executive ?? When (MS )? "+ (Datetime. Now-begin). totalmilliseconds );
  45. Console. writeline ("");
  46.  
  47. Console. writeline ("director ends. ");

Result:

As shown in, after the Director service is suspended, wait until the sub-task is executed and continue to execute the following code segment (that is, the director service ).

 

Code:/files/wsky/parallev1.0.rar

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.