[源码下载]

重新想象 Windows 8 Store Apps (48) - 多线程之其他辅助类: SpinWait, SpinLock, Volatile, SynchronizationContext, CoreDispatcher, ThreadLocal, ThreadStaticAttribute

作者:webabcd

介绍
重新想象 Windows 8 Store Apps 之 多线程操作的其他辅助类

  • SpinWait - 自旋等待
  • SpinLock - 自旋锁
  • volatile - 必在内存
  • SynchronizationContext - 在指定的线程上同步数据
  • CoreDispatcher - 调度器,用于线程同步
  • ThreadLocal - 用于保存每个线程自己的数据
  • ThreadStaticAttribute - 所指定的静态变量对每个线程都是唯一的

示例
1、演示 SpinWait 的使用
Thread/Other/SpinWaitDemo.xaml.cs

/*
* 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;
}
// 如果此超时时间过后指定的条件还未成立,则强制结束等待
,); lblMsg.Text += Environment.NewLine;
lblMsg.Text += DateTime.Now.ToString("mm:ss.fff"); SpinWait.SpinUntil(
() => // 以下条件成立时,结束等待
{
return DateTime.Now.Second % == ;
}); lblMsg.Text += Environment.NewLine;
lblMsg.Text += DateTime.Now.ToString("mm:ss.fff");
}
}
}

2、演示 SpinLock 的使用
Thread/Other/SpinLockDemo.xaml.cs

/*
* 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 = ; i < ; i++)
{
Task task = Task.Run(
() =>
{
bool lockTaken = false; try
{
// IsHeld - 锁当前是否已由任何线程占用
// IsHeldByCurrentThread - 锁是否由当前线程占用
// 要获取 IsHeldByCurrentThread 属性,则 IsThreadOwnerTrackingEnabled 必须为 true,可以在构造函数中指定,默认就是 true // 进入锁,lockTaken - 是否已获取到锁
spinLock.Enter(ref lockTaken); for (int j = ; j < ; j++)
{
_count++;
}
}
finally
{
// 释放锁
if (lockTaken)
spinLock.Exit();
}
}); tasks.Add(task);
} // 等待所有任务执行完毕
await Task.WhenAll(tasks); lblMsg.Text = "count: " + _count.ToString();
}
}
}

3、演示 volatile 的使用
Thread/Other/VolatileDemo.xaml

<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>

4、演示 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());
}
}
}

5、演示 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());
}
}
}

6、演示 ThreadLocal 的使用
Thread/Other/ThreadLocalDemo.xaml.cs

/*
* 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;
}
}
}

7、演示 ThreadStaticAttribute 的使用
Thread/Other/ThreadStaticAttributeDemo.xaml.cs

/*
* 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 = ; System.Threading.SynchronizationContext _syncContext; public ThreadStaticAttributeDemo()
{
this.InitializeComponent(); // 获取当前线程,即 UI 线程
_syncContext = System.Threading.SynchronizationContext.Current;
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
Task.Run(() =>
{
_testValue = ; _syncContext.Post((testValue) =>
{
lblMsg.Text += Environment.NewLine;
// 此 Task 上的 _testValue 的值
lblMsg.Text += "thread 1 testValue: " + testValue.ToString();
},
_testValue);
}); Task.Run(() =>
{
_testValue = ; _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
[源码下载]

重新想象 Windows 8 Store Apps (48) - 多线程之其他辅助类: SpinWait, SpinLock, Volatile, SynchronizationContext, CoreDispatcher, ThreadLocal, ThreadStaticAttribute的更多相关文章

  1. 重新想象 Windows 8 Store Apps (42) - 多线程之线程池: 延迟执行, 周期执行, 在线程池中找一个线程去执行指定的方法

    [源码下载] 重新想象 Windows 8 Store Apps (42) - 多线程之线程池: 延迟执行, 周期执行, 在线程池中找一个线程去执行指定的方法 作者:webabcd 介绍重新想象 Wi ...

  2. 重新想象 Windows 8 Store Apps (43) - 多线程之任务: Task 基础, 多任务并行执行, 并行运算(Parallel)

    [源码下载] 重新想象 Windows 8 Store Apps (43) - 多线程之任务: Task 基础, 多任务并行执行, 并行运算(Parallel) 作者:webabcd 介绍重新想象 W ...

  3. 重新想象 Windows 8 Store Apps (44) - 多线程之异步编程: 经典和最新的异步编程模型, IAsyncInfo 与 Task 相互转换

    [源码下载] 重新想象 Windows 8 Store Apps (44) - 多线程之异步编程: 经典和最新的异步编程模型, IAsyncInfo 与 Task 相互转换 作者:webabcd 介绍 ...

  4. 重新想象 Windows 8 Store Apps (45) - 多线程之异步编程: IAsyncAction, IAsyncOperation, IAsyncActionWithProgress, IAsyncOperationWithProgress

    [源码下载] 重新想象 Windows 8 Store Apps (45) - 多线程之异步编程: IAsyncAction, IAsyncOperation, IAsyncActionWithPro ...

  5. 重新想象 Windows 8 Store Apps (46) - 多线程之线程同步: Lock, Monitor, Interlocked, Mutex, ReaderWriterLock

    [源码下载] 重新想象 Windows 8 Store Apps (46) - 多线程之线程同步: Lock, Monitor, Interlocked, Mutex, ReaderWriterLoc ...

  6. 重新想象 Windows 8 Store Apps (47) - 多线程之线程同步: Semaphore, CountdownEvent, Barrier, ManualResetEvent, AutoResetEvent

    [源码下载] 重新想象 Windows 8 Store Apps (47) - 多线程之线程同步: Semaphore, CountdownEvent, Barrier, ManualResetEve ...

  7. 重新想象 Windows 8 Store Apps 系列文章索引

    [源码下载][重新想象 Windows 8.1 Store Apps 系列文章] 重新想象 Windows 8 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...

  8. 重新想象 Windows 8 Store Apps (56) - 系统 UI: Scale, Snap, Orientation, High Contrast 等

    [源码下载] 重新想象 Windows 8 Store Apps (56) - 系统 UI: Scale, Snap, Orientation, High Contrast 等 作者:webabcd ...

  9. 重新想象 Windows 8 Store Apps (30) - 信息: 获取包信息, 系统信息, 硬件信息, PnP信息, 常用设备信息

    原文:重新想象 Windows 8 Store Apps (30) - 信息: 获取包信息, 系统信息, 硬件信息, PnP信息, 常用设备信息 [源码下载] 重新想象 Windows 8 Store ...

随机推荐

  1. Win10下E3-1231 V3开启Intel虚拟化技术(vt-x)安装HAXM

    硬件配置: 技嘉G1 Sniper B6主板,Intel Xeon E3-1231 V3 CPU.主板和U都支持Intel的虚拟化技术,也在主板的设置界面打开了虚拟化支持,如下图: 使用CPU-V检测 ...

  2. Spark源码系列(五)分布式缓存

    这一章想讲一下Spark的缓存是如何实现的.这个persist方法是在RDD里面的,所以我们直接打开RDD这个类. def persist(newLevel: StorageLevel): this. ...

  3. getRequestURI,getRequestURL的区别

    转自:http://www.cnblogs.com/JemBai/archive/2010/11/10/1873764.html test1.jsp======================= &l ...

  4. ODAC (V9.5.15) 学习笔记(二十一)数据复制

    用TVirtualTable在内存中缓存TOraQuery中的数据,主要应用场景是参照其他数据,需要将TOraQuery中的数据复制到TVirtualTable,由于没有类似于TClientDataS ...

  5. 使用jQuery开发一个带有密码强度检验的超酷注册页面

    在今天的jQuery教程中,我们将介绍如何使用jQuery和其它相关的插件来生成一个漂亮的带有密码强度检验的注册页面,希望大家喜欢! 相关的插件和类库 complexify - 一个密码强度检验jQu ...

  6. ios ZBar扫二维码奇奇怪怪的错误

    Undefined symbols for architecture armv7: "_CVPixelBufferGetHeight", referenced from: -[ZB ...

  7. JAVA字节码解析

    Java字节码指令 Java 字节码指令及javap 使用说明 ### java字节码指令列表 字节码 助记符 指令含义 0x00 nop 什么都不做 0x01 aconst_null 将null推送 ...

  8. 三十、【C#.Net开发框架】WCFHosting服务主机的利用WCF服务通讯和实现思路

    回<[开源]EFW框架系列文章索引>        EFW框架源代码下载V1.3:http://pan.baidu.com/s/1c0dADO0 EFW框架实例源代码下载:http://p ...

  9. C#简易播放器(基于开源VLC)

    可见光通信技术(Visible Light Communication,VLC)是指利用可见光波段的光作为信息载体,不使用光纤等有线信道的传输介质,而在空气中直接传输光信号的通信方式.LED可见光通信 ...

  10. 在server 2008/2003中 取消对网站的安全检查/去除添加信任网站

    新安装好Windows   Server   2003操作系统后,打开浏览器来查询网上信息时,发现IE总是“不厌其烦”地提示我们,是否需要将当前访问的网站添加到自己信任的站点中去:要是不信任的话,就无 ...