Windows 8 Store Apps Learning (47) Multi-thread thread synchronization: semaphore etc.

Source: Internet
Author: User
Tags semaphore thread xmlns

Multi-thread thread synchronization: Semaphore, Countdownevent, Barrier, ManualResetEvent, AutoResetEvent

Introduced

Re-visualize the thread sync for Windows 8 Store Apps

Semaphore-Signal Volume

Countdownevent-thread synchronization via semaphore number

Barrier-Barrier

ManualResetEvent-Manual traffic lights

AutoResetEvent-Automatic traffic lights

Example

1. Demonstrate the use of semaphore

Thread/lock/semaphoredemo.xaml

<page
    x:class= "XamlDemo.Thread.Lock.SemaphoreDemo"
    xmlns= "http://schemas.microsoft.com/winfx/2006/ Xaml/presentation "
    xmlns:x=" Http://schemas.microsoft.com/winfx/2006/xaml "
    xmlns:local=" using: XamlDemo.Thread.Lock "
    xmlns:d=" http://schemas.microsoft.com/expression/blend/2008 "
    xmlns:mc=" http:// schemas.openxmlformats.org/markup-compatibility/2006 "
    mc:ignorable=" D ">
    
    <grid background=" Transparent ">
        <stackpanel margin=" 0 0 0 ">
    
            <textblock name=" lblmsg "fontsize=" 14.667 "/>
    
        </StackPanel>
    </Grid>
</Page>

Thread/lock/semaphoredemo.xaml.cs

* * Demo Semaphore use * * semaphore-semaphore * Semaphoreslim-Lightweight Semaphore * * Note: * Literal translation semaphore words not very good understanding, can be Sem Aphore understood as a license center, the number of licenses in the License center is limited * threads want to execute first obtain a license from the License Center (if the license center's license has been issued, wait, wait for the other thread to return the license), and then go back./using Sy
Stem
Using System.Collections.Generic;
Using System.Threading;
Using System.Threading.Tasks;
Using Windows.UI.Xaml.Controls;
    
Using Windows.UI.Xaml.Navigation; Namespace XamlDemo.Thread.Lock {public sealed partial class Semaphoredemo:page {* * * semaphor E (int initialcount, int maximumcount, string name) * Initialcount-the number of licenses initially owned by the license center, that is, the number of licenses already issued in the initial case is MAXIMUMC Ount-initialcount * Maximumcount-Total number of licenses owned by the license centre * name-License Center name * Semaphore Open Existing (string name)-Opens the license center with the specified name//instantiates a license center with a license number of 2 private semaphore _se
    
        Maphore = new Semaphore (2, 2); Public Semaphoredemo () {this. InitializEcomponent (); } protected async override void Onnavigatedto (NavigationEventArgs e) {list<task> T
    
            asks = new list<task> (); Simulate 5 threads executing in parallel, the thread that gets the license to run, and the license center has only 2 licenses for (int i = 0; i < 5; i++) {Canc Ellationtoken token = new CancellationTokenSource ().
    
                Token; Task task = Task.run (() => {outmsg () string.
                        Format ("task {0} waits for a license", Task.currentid)); Token.
    
                        WaitHandle.WaitOne (5000); WaitOne ()-Apply for license _semaphore.
                        WaitOne (); Outmsg (String.
    
                        Format ("task {0} applies to a license", Task.currentid)); Token.
    
                        WaitHandle.WaitOne (1000); Outmsg (String.
                        Format ("task {0} returned a license", Task.currentid)); int release ()-return license, return value is: Release ()Number of licenses available to the license center int ignored = _semaphore.
                        Release ();
    
                int release (int releasecount)-Specifies the number of semaphores to be released (as per this article, specify the number of licenses returned)}, token); Tasks.
            ADD (Task);
        } await Task.whenall (tasks); } private void Outmsg (String msg) {var ignored = Dispatcher.runasync (Wi Ndows. Ui.
                    Core.CoreDispatcherPriority.High, () => {lblmsg.text = msg;
                Lblmsg.text + = Environment.NewLine;
        }); }
    }
}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.