C # multi-thread Parallel is similar to the for continue and break method,

Source: Internet
Author: User

C # multi-thread Parallel is similar to the for continue and break method,

I haven't written anything for a long time, and finally found some knowledge records...

The ParallelLoopState object is used to control the execution of the Parallel. For function. The ParallelLoopState object is created in the background at runtime:

Parallel.For(1, 100, (i, ParallelLoopState) =>{...});

This object has two functions: Stop and Break, which can be used to control the execution of Parallel. For respectively.

When you call Stop, the execution of Parallel. For is immediately stopped, regardless of whether other execution units have reached the Stop condition. If we use Break, it indicates that the current execution unit meeting the condition is stopped immediately, and for other execution units, where the condition is met, it will also be stopped through Break, if the other conditions are not met, the execution will continue, so that all the execution is completed and the operation will naturally stop. After all execution units are stopped, the Parallel. For function is stopped and exited.

Break is similar to the for continue, while stop is similar to the for break.

Parallel. for (1,100, (I, ParallelLoopState) => {// when the number of a loop unit is greater than 30, // The current execution unit is displayed, wait for other execution units to end // All execution units to end and then exit Parallel. for execution if (I> 30) {// jumps out of the current execution unit ParallelLoopState. break (); return; // if return is not added, the process resource may not be released. }});
Parallel. for (1,100, (I, ParallelLoopState) => {// when the number of a loop unit is greater than 30, // stop Parallel. for execution if (I> 30) {// stop and exit Parallel. for ParallelLoopState. stop (); return ;}});

The functions of the ParallelLoopState object in Parallel. ForEach are also called in the same way:

1             Parallel.ForEach(elements, (element, ParallelLoopState) =>2             {3                 if (element == "")4                 {5                     ParallelLoopState.Break();6                     return;7                 }8             });

 


Symbol in C Language <Yes

Left shift operator (<)

Removes all the binary bits of an operation object from the left and adds 0 to the right ).

For example, a = a <2 shifts the binary bits of a two places to the left and complements 0 to the right,

Move 1 to the left and then a = a * 2;

If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.
Shift right operator (>)

Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.

The operand shifts one digit to the right, which is equivalent to dividing the number by 2.

For example, a = a> 2 shifts the binary bit of a two places to the right,

0 or 1 to see whether the number is positive or negative.

Symbol in C Language <Yes

Left shift operator (<)

Removes all the binary bits of an operation object from the left and adds 0 to the right ).

For example, a = a <2 shifts the binary bits of a two places to the left and complements 0 to the right,

Move 1 to the left and then a = a * 2;

If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.
Shift right operator (>)

Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.

The operand shifts one digit to the right, which is equivalent to dividing the number by 2.

For example, a = a> 2 shifts the binary bit of a two places to the right,

0 or 1 to see whether the number is positive or negative.

Related Article

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.