"C # Multithreaded Programming combat" 2.8 Barrier

Source: Internet
Author: User

It has to be said that the mechanism of synchronous threading in C # is really much.

Various. There are synchronization mechanisms that can be used in almost every scenario.

What I said today is more interesting.

The mechanism of waiting is very simple, waiting simply. I'll wait for the way I use it. The things you wait for or the content you set yourself.

Quite well understood.

This thing in the book has a bigger role in iteration.

  classProgram {StaticBarrier Barrier =NewBarrier (1, B=>console.writeline ($"End of Phase{b.currentphasenumber+1}")); Static voidPlaymusic (stringNamestringMessageintseconds) {             for(inti =2; I <3; i++) {Console.WriteLine ("----------------------------------------------------");                Thread.Sleep (timespan.frommilliseconds (seconds)); Console.WriteLine ($"{Name} finishes to {message}"); Barrier.            SignalAndWait (); }        }        Static voidMain (string[] args) {            varT1 =NewThread (() = Playmusic ("M1","Good Music",5)); vart2 =NewThread (() = Playmusic ("m2","Bad music",5)); T1.            Start (); T2.            Start ();        Console.readkey (); }    }

The special place is when barrier the arguments to the constructor at the time of instantiation,

The first parameter is a few times or is understood to have several different places or threads using barrier

The second parameter is nullable and is a callback function. When to use it is called when barrier uses the SignalAndWait method. Wait for the content is also here

The rest is to see the SignalAndWait method.

"C # Multithreaded Programming combat" 2.8 Barrier

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.