My journey to Cr (2): Open the door to Cr and write a multi-threaded program that does not require thread creation or resource mutex.

Source: Internet
Author: User

At the beginning of this article, we will give a few simple examples to gradually learn about Cr and see how amazing it is.

I. First magic: no multi-thread writing is required for APIS RELATED TO THE CREATION thread and resource mutex.Program

This example is a very simple console. She will face millions of data submitted in an instant, without changing the color (CPU and memory are very stable). The queue will always only store the latest data, only CPU data is processed at a time (my machine is dual-core, so here I am, each CPU is a thread, and the real parallel operation is done ....), OK, no nonsense. Go to the question:

Er, since it is an instance, you can directly view it.CodeOkay:

Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. Threading;
Using Microsoft. cr. core;

Namespace Ccrdemo1
{
Class Program
{
Static   Void Main ( String [] ARGs)
{
Int Maxiqueuedepth =   10 ;
// Step 1: Create a dispatcher object
Dispatcher =   New DISPATCHER ( 0 , " Scheduler name " );
// Step 2: Create a dispatcherqueue object associated with the step 1 Object
Dispatcherqueue depththrottledqueue =   New Dispatcherqueue (
" Task queue name " ,
// Scheduler associated with the queue
Dispatcher,
// Data storage policy in the queue: recent message storage policy
Taskexecutionpolicy. constrainqueuedepthdiscardtasks,
// Queue depth
Maxiqueuedepth
);
// Step 3: Create a port that can receive Integer Data
Port < Int > Intport =   New Port < Int > ();
// Step 4: Associate the port with the processing function and then dispatcherqueue
Arbiter. Activate (depththrottledqueue,
Arbiter. Receive ( True ,
Intport,
Delegate ( Int I) // An anonymous method is used as a processing function.
{
Thread. Sleep ( 2000 );
Console. writeline ( " [{0}] {1} " , Datetime. Now. tostring ( " O " ), I );
}
)
);

// Step 5: Quickly submit a large number of tasks
Console. writeline ( " [{0}] Start to submit a large number of tasks " , Datetime. Now. tostring ( " O " ));
For ( Int I =   0 ; I < Maxiqueuedepth *   100000 ; I ++ )
{
// Post Data to intport
Intport. Post (I );
}
Console. writeline ( " [{0}] a large number of tasks have been submitted. " , Datetime. Now. tostring ( " O " ));

Console. writeline ("Press any key to exit");
Console. readkey ();
Dispatcher. Dispose ();
}
}
}

 

 Ii. Principles

In fact, I think I should understand the comments in the code, but after all, it is the first example. I will give a little attention to the general principle of the CR (as for the detailed implementation principle, I will talk about it later, but the main purpose of this time is to first appreciate the magic of the CR ):

First, multithreading is managed by the dispatcher in the form of the thread pool of the operating system. Therefore, the thread creation work is actually done by the dispatcher;

Secondly, in the CCR, it is stipulated that dispatcher can only process tasks, and this task can only be obtained from the dispatcherqueue of the task queue. Therefore, we need to create a task queue and associate it with dispatcher;

Then, we encapsulate the data we want to submit to the processing function for processing in the port <t>. The port is actually a FIFO queue, which is used to receive the data submitted by the user;

Finally, we need to combine data, processing functions, and task queues. This is step 4 in the code above. In this step, we actually did two jobs:

1. encapsulate the port and processing function as receive and associate them;

2. Associate receive with dispatcherqueue;

In this way, we have finished all the work.

In short, the CR provides a mode, so that we only need to split the jobs that require concurrent and asynchronous processing:

1. Input Data ---> post to port;

2. handling process ---> make a delegate and associate it with the task queue

This method greatly simplifies the compilation of multi-threaded programs and allows the background code to be optimized by the compiler.

Iii. Appendix

1. Download instance code

2. previous articles in this series:

My journey to CCR (1): New concurrent and asynchronous solutions from Microsoft Robotics

 

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.