小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Windows8線程間通信與同步

 牛人的尾巴 2015-12-01

Windows8線程間通信與同步

(2013-03-19 09:09:58)
       private int interLocked = 9;

       private int lockInt = 19;
       private object lockObj = new object();

       private Mutex mutex;

       private Semaphore semaphore = new Semaphore(2, 2);

        public MainPage()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            //線程同步: Interlocked
            ThreadPool.RunAsync(
                (source) =>
                {
                    Interlocked.Increment(ref interLocked);
                    //Interlocked.Decrement(ref interLocked);
                },
            WorkItemPriority.Normal);

            //lock
            ThreadPool.RunAsync(
                (source) =>
                {
                    lock (lockObj)
                    {
                        lockInt++;
                        lockInt *= 5;
                    }
                },
            WorkItemPriority.Normal);

            //Monitor
            ThreadPool.RunAsync(
                (source) =>
                {
                    Monitor.Enter(lockObj);

                    Monitor.Wait(lockObj);//釋放對象上的鎖并進入對象的等待隊列
                    //Monitor.Pulse(lockObj);//通知等待隊列中的線程鎖定對象狀態(tài)的更改

                    Monitor.Exit(lockObj);//釋放指定對象上的鎖
                },
            WorkItemPriority.Normal);


            //Mutex
            ThreadPool.RunAsync(
                (source) =>
                {
                    bool isNotRunning;
                    mutex = new Mutex(true, "MSI-Muxtex", out isNotRunning);
                    if (!isNotRunning)
                    {
                        //Do Something ...
                    }
                },
            WorkItemPriority.Normal);

            
            //Semaphore
            ThreadPool.RunAsync(
                (source) =>
                {
                    //阻止當(dāng)前線程,直到當(dāng)前WaitHandle收到信號
                    semaphore.WaitOne();

                    //退出信號量并返回前一個計數(shù)
                    semaphore.Release();
                },
            WorkItemPriority.Normal);

            //AutoResetEvent autoRestEvent = new AutoResetEvent(false);
            //autoRestEvent.Set();
            //ManualResetEvent manualRestEvent = new ManualResetEvent(false);
            //manualRestEvent.Set();
            //manualRestEvent.Reset();
            EventWaitHandle eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, "msiEventWaitHandle");
            eventWaitHandle.WaitOne();
            eventWaitHandle.Set();
            eventWaitHandle.Reset();

        }

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多