從新想象 Windows 8 Store Apps (48) - 多線程之其餘輔助類: SpinWait, SpinLock, Volatile, SynchronizationContext, Co

[源碼下載]


html

從新想象 Windows 8 Store Apps (48) - 多線程之其餘輔助類: SpinWait, SpinLock, Volatile, SynchronizationContext, CoreDispatcher, ThreadLocal, ThreadStaticAttribute



做者:webabcd


介紹
從新想象 Windows 8 Store Apps 之 多線程操做的其餘輔助類html5

  • SpinWait - 自旋等待
  • SpinLock - 自旋鎖
  • volatile - 必在內存
  • SynchronizationContext - 在指定的線程上同步數據
  • CoreDispatcher - 調度器,用於線程同步
  • ThreadLocal - 用於保存每一個線程本身的數據
  • ThreadStaticAttribute - 所指定的靜態變量對每一個線程都是惟一的



示例
一、演示 SpinWait 的使用
Thread/Other/SpinWaitDemo.xaml.csweb

/*
 * SpinWait - 自旋等待,一個低級別的同步類型。它不會放棄任何 cpu 時間,而是讓 cpu 不停的循環等待
 * 
 * 適用場景:多核 cpu ,預期等待時間很是短(幾微秒)
 * 本例只是用於描述 SpinWait 的用法,而不表明適用場景
 */

using System;
using System.Threading;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace XamlDemo.Thread.Other
{
    public sealed partial class SpinWaitDemo : Page
    {
        public SpinWaitDemo()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            lblMsg.Text = DateTime.Now.ToString("mm:ss.fff");

            SpinWait.SpinUntil(
                () =>  // 如下條件成立時,結束等待
                {
                    return false;
                }
                // 若是此超時時間事後指定的條件還未成立,則強制結束等待
                ,1000);

            lblMsg.Text += Environment.NewLine;
            lblMsg.Text += DateTime.Now.ToString("mm:ss.fff");

            SpinWait.SpinUntil(
                () => // 如下條件成立時,結束等待
                {
                    return DateTime.Now.Second % 2 == 0;
                });

            lblMsg.Text += Environment.NewLine;
            lblMsg.Text += DateTime.Now.ToString("mm:ss.fff");
        }
    }
}


二、演示 SpinLock 的使用
Thread/Other/SpinLockDemo.xaml.csexpress

/*
 * SpinLock - 自旋鎖,一個低級別的互斥鎖。它不會放棄任何 cpu 時間,而是讓 cpu 不停的循環等待,直至鎖變爲可用爲止
 * 
 * 適用場景:多核 cpu ,預期等待時間很是短(幾微秒)
 * 本例只是用於描述 SpinLock 的用法,而不表明適用場景
 */

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace XamlDemo.Thread.Other
{
    public sealed partial class SpinLockDemo : Page
    {
        private static int _count;

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

        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            SpinLock spinLock = new SpinLock();

            List<Task> tasks = new List<Task>();

            // 一共 100 個任務並行執行,每一個任務均累加同一個靜態變量 100000 次,以模擬併發訪問靜態變量的場景
            for (int i = 0; i < 100; i++)
            {
                Task task = Task.Run(
                    () =>
                    {
                        bool lockTaken = false;

                        try
                        {
                            // IsHeld - 鎖當前是否已由任何線程佔用
                            // IsHeldByCurrentThread - 鎖是否由當前線程佔用
                            //     要獲取 IsHeldByCurrentThread 屬性,則 IsThreadOwnerTrackingEnabled 必須爲 true,能夠在構造函數中指定,默認就是 true

                            // 進入鎖,lockTaken - 是否已獲取到鎖
                            spinLock.Enter(ref lockTaken);

                            for (int j = 0; j < 100000; j++)
                            {
                                _count++;
                            }
                        }
                        finally
                        {
                            // 釋放鎖
                            if (lockTaken)
                                spinLock.Exit();
                        }
                    });

                tasks.Add(task);
            }

            // 等待全部任務執行完畢
            await Task.WhenAll(tasks);

            lblMsg.Text = "count: " + _count.ToString();
        }
    }
}


三、演示 volatile 的使用
Thread/Other/VolatileDemo.xamlwindows

<Page
    x:Class="XamlDemo.Thread.Other.VolatileDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:XamlDemo.Thread.Other"
    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="120 0 0 0">

            <TextBlock FontSize="14.667" LineHeight="20">
                <Run>若是編譯器認爲某字段無外部修改,則爲了優化會將其放入寄存器</Run>
                <LineBreak />
                <Run>標記爲 volatile 的字段,則必然會被放進內存</Run>
                <LineBreak />
                <Run>編寫 Windows Store Apps 後臺任務類的時候,若是某個字段會被後臺任務的調用者修改的話,就要將其標記爲 volatile,由於這種狀況下編譯器會認爲此字段無外部修改</Run>
            </TextBlock>

        </StackPanel>
    </Grid>
</Page>


四、演示 SynchronizationContext 的使用
Thread/Other/SynchronizationContextDemo.xaml.cs多線程

/*
 * SynchronizationContext - 在指定的線程上同步數據
 *     Current - 獲取當前線程的 SynchronizationContext 對象
 *     Post(SendOrPostCallback d, object state) - 同步數據到此 SynchronizationContext 所關聯的線程上
 */

using System;
using Windows.System.Threading;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace XamlDemo.Thread.Other
{
    public sealed partial class SynchronizationContextDemo : Page
    {
        System.Threading.SynchronizationContext _syncContext;

        public SynchronizationContextDemo()
        {
            this.InitializeComponent();

            // 獲取當前線程,即 UI 線程
            _syncContext = System.Threading.SynchronizationContext.Current;

            ThreadPoolTimer.CreatePeriodicTimer(
               (timer) =>
               {
                   // 在指定的線程(UI 線程)上同步數據
                   _syncContext.Post(
                       (ctx) =>
                       {
                           lblMsg.Text = DateTime.Now.ToString("mm:ss.fff");
                       },
                       null);
               },
               TimeSpan.FromMilliseconds(100));
        }
    }
}


五、演示 CoreDispatcher 的使用
Thread/Other/CoreDispatcherDemo.xaml.cs併發

/*
 * CoreDispatcher - 調度器,用於線程同步
 */

using System;
using Windows.System.Threading;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace XamlDemo.Thread.Other
{
    public sealed partial class CoreDispatcherDemo : Page
    {
        public CoreDispatcherDemo()
        {
            this.InitializeComponent();

            // 獲取 UI 線程的 CoreDispatcher
            CoreDispatcher dispatcher = Window.Current.Dispatcher;

            ThreadPoolTimer.CreatePeriodicTimer(
               (timer) =>
               {
                   // 經過 CoreDispatcher 同步數據
                   // var ignored = this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                   var ignored = dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                       () =>
                       {
                           lblMsg.Text = DateTime.Now.ToString("mm:ss.fff");
                       });
               },
               TimeSpan.FromMilliseconds(100));
        }
    }
}


六、演示 ThreadLocal 的使用
Thread/Other/ThreadLocalDemo.xaml.csapp

/*
 * ThreadLocal<T> - 用於保存每一個線程本身的數據,T - 須要保存的數據的數據類型
 *     ThreadLocal(Func<T> valueFactory, bool trackAllValues) - 構造函數
 *         valueFactory - 指定當前線程個性化數據的初始值
 *         trackAllValues - 是否須要獲取全部線程的個性化數據
 *     T Value - 當前線程的個性化數據
 *     IList<T> Values - 獲取全部線程的個性化數據(trackAllValues == true 才能獲取)
 *     
 * 
 * 注:ThreadStaticAttribute 與 ThreadLocal<T> 的做用差很少
 */

using System;
using System.Threading;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace XamlDemo.Thread.Other
{
    public sealed partial class ThreadLocalDemo : Page
    {
        System.Threading.SynchronizationContext _syncContext;

        public ThreadLocalDemo()
        {
            this.InitializeComponent();

            // 獲取當前線程,即 UI 線程
            _syncContext = System.Threading.SynchronizationContext.Current;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ThreadLocal<string> localThread = new ThreadLocal<string>(
                () => "ui thread webabcd", // ui 線程的個性化數據
                false);

            Task.Run(() =>
            {
                // 此任務的個性化數據
                localThread.Value = "thread 1 webabcd";

                _syncContext.Post((ctx) =>
                {
                    lblMsg.Text += Environment.NewLine;
                    lblMsg.Text += ctx.ToString();
                },
                localThread.Value);
            });

            Task.Run(() =>
            {
                // 此任務的個性化數據
                localThread.Value = "thread 2 webabcd";

                _syncContext.Post((ctx) =>
                {
                    lblMsg.Text += Environment.NewLine;
                    lblMsg.Text += ctx.ToString();
                },
                localThread.Value);
            });

            lblMsg.Text += localThread.Value;
        }
    }
}


七、演示 ThreadStaticAttribute 的使用
Thread/Other/ThreadStaticAttributeDemo.xaml.csasp.net

/*
 * ThreadStaticAttribute - 所指定的靜態變量對每一個線程都是惟一的
 * 
 * 
 * 注:ThreadStaticAttribute 與 ThreadLocal<T> 的做用差很少
 */

using System;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;

namespace XamlDemo.Thread.Other
{
    public sealed partial class ThreadStaticAttributeDemo : Page
    {
        // 此靜態變量對每一個線程都是惟一的
        [ThreadStatic]
        private static int _testValue = 0;

        System.Threading.SynchronizationContext _syncContext;

        public ThreadStaticAttributeDemo()
        {
            this.InitializeComponent();

            // 獲取當前線程,即 UI 線程
            _syncContext = System.Threading.SynchronizationContext.Current;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            Task.Run(() =>
            {
                _testValue = 10;

                _syncContext.Post((testValue) =>
                {
                    lblMsg.Text += Environment.NewLine;
                    // 此 Task 上的 _testValue 的值
                    lblMsg.Text += "thread 1 testValue: " + testValue.ToString();
                },
                _testValue);
            });

            Task.Run(() =>
            {
                _testValue = 100;

                _syncContext.Post((testValue) =>
                {
                    lblMsg.Text += Environment.NewLine;
                    // 此 Task 上的 _testValue 的值
                    lblMsg.Text += "thread 2 testValue: " + testValue.ToString();
                },
                _testValue);
            });

            // ui 線程上的 _testValue 的值
            lblMsg.Text = "ui thread testValue: " + _testValue.ToString();
        }
    }
}



OK
[源碼下載]async

相關文章
相關標籤/搜索