2.1 简介

竞争条件:多个线程同时使用共享对象。需要同步这些线程使得共享对象的操作能够以正确的顺序执行

线程同步问题:多线程的执行并没有正确的同步,当一个线程执行递增和递减操作时,其他线程需要依次等待

线程同步解决方案:

无须共享对象:大部分时候可以通过重新设计来移除共享对象,去掉复杂的同步构造,避免多线程使用单一对象

必须共享对象:只使用原子操作,一个操作只占用一个量子的时间,无须实现其他线程等待当前操作完成

内核模式:将等待的线程置于阻塞状态,消耗少量的CPU资源,但会引入至少一次上下文切换,适用于线程等待较长时间

用户模式:只是简单的等待,线程等待会浪费CPU时间但是可以节省上下文切换消耗的CPU时间,适用于线程等待较短时间

混合模式:先尝试用户模式,如果等待时间较长,则会切换到内核模式

2.2 执行基本的原子操作

using System;
using System.Threading; namespace MulityThreadNote
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Incorrect counter");
//没有限定,会遇到竞争条件,得出的结果大部分不是正确的
var c = new Counter();
var t1 = new Thread(() => TestCounter(c));
var t2 = new Thread(() => TestCounter(c));
var t3 = new Thread(() => TestCounter(c));
t1.Start();
t2.Start();
t3.Start();
t1.Join();
t2.Join();
t3.Join();
Console.WriteLine($"Total count:{c.Count}");
Console.WriteLine("--------------------------");
Console.WriteLine("Correct counter");
//使用Interlocked类提供的原子操作方法,无需锁定任何对象可得出正确结果
var c1 = new CounterNoLock();
t1 = new Thread(() => TestCounter(c1));
t2 = new Thread(() => TestCounter(c1));
t3 = new Thread(() => TestCounter(c1));
t1.Start();
t2.Start();
t3.Start();
t1.Join();
t2.Join();
t3.Join();
Console.WriteLine($"Total count:{c1.Count}");
Console.ReadLine();
}
static void TestCounter(CounterBase c)
{
for (int i = ; i < ; i++)
{
c.Increment();
c.Decrement();
}
}
class Counter : CounterBase
{
private int _count;
public int Count { get { return _count; } } public override void Decrement()
{
_count--;
} public override void Increment()
{
_count++;
}
} class CounterNoLock : CounterBase
{
private int _count;
public int Count { get { return _count; } } public override void Decrement()
{
//Interlocked提供了Increment()、Decrement()和Add等基本数学操作的原子方法
Interlocked.Decrement(ref _count);
} public override void Increment()
{
Interlocked.Increment(ref _count);
}
} abstract class CounterBase
{
public abstract void Increment();
public abstract void Decrement();
}
}
}

注释:Interlocked提供了Increment()、Decrement()和Add等基本数学操作的原子方法,不用锁也可以得出正确结果

2.3 使用Mutex类

using System;
using System.Threading; namespace MulityThreadNote
{
class Program
{
static void Main(string[] args)
{
const string MutexName = "CSharpThreadingCookbook";
//Mutex是一种原始的同步方式,只对一个线程授予对共享资源的独占访问
//定义一个指定名称的互斥量,设置initialOwner标志为false
using (var m = new Mutex(false, MutexName))
{
//如果互斥量已经被创建,获取互斥量,否则就执行else语句
if (!m.WaitOne(TimeSpan.FromSeconds(), false))
{
Console.WriteLine("Second instance is running!");
}
else
{
Console.WriteLine("Running!");
Console.ReadLine();
m.ReleaseMutex();
}
}
//如果再运行同样的程序,则会在5秒内尝试获取互斥量,如果第一个程序按下了任何键,第二个程序开始执行。
//如果等待5秒钟,第二个程序将无法获取该互斥量
}
}
}

注释:互斥量是全局操作对象,必须正确关闭,最好用using

2.4 使用SemaphoreSlim类

using System;
using System.Threading; namespace MulityThreadNote
{
class Program
{
static void Main(string[] args)
{
//启动6个线程,启动的顺序不一样
for (int i = ; i <= ; i++)
{
string threadName = "Thread " + i;
int secondsToWait = + * i;
var t = new Thread(() => AccessDatabase(threadName, secondsToWait));
t.Start();
} Console.ReadLine();
}
//SemaphoreSlim的构造函数参数为允许的并发线程数目
static SemaphoreSlim semaphore = new SemaphoreSlim(); static void AccessDatabase(string name, int seconds)
{
Console.WriteLine($"{name} waits to access a database");
semaphore.Wait();
Console.WriteLine($"{name} was granted an access to a database");
Thread.Sleep(TimeSpan.FromSeconds(seconds));
Console.WriteLine($"{name} is Completed");
//调用Release方法说明线程已经完成,可以开启一个新的线程了
semaphore.Release();
}
}
}

注释:这里使用了混合模式,允许我们在等待时间很短的情况下无需上下文切换。SemaphoreSlim并不使用Windows内核信号量,而且也不支持进程间同步

2.5 使用AutoResetEvent类

using System;
using System.Threading; namespace MulityThreadNote
{
class Program
{
static void Main(string[] args)
{
Thread t = new Thread(() => Process());
t.Start();
Console.WriteLine("Waiting for another thread to complete work");
//开启一个线程后workEvent等待,直到收到Set信号
workEvent.WaitOne();
Console.WriteLine("First operation is complete");
Console.WriteLine("Performing an operation on a main thread");
Thread.Sleep(TimeSpan.FromSeconds());
mainEvent.Set();
Console.WriteLine("Now running the second operation on a second thread");
workEvent.WaitOne();
Console.WriteLine("Second operation is complete");
Console.ReadLine();
}
//初始状态为unsignaled,子线程向主线程发信号
private static AutoResetEvent workEvent = new AutoResetEvent(false);
//初始状态为unsignaled,主线程向子线程发信号
private static AutoResetEvent mainEvent = new AutoResetEvent(false); static void Process(int seconds)
{
Console.WriteLine("Starting a long running work...");
Thread.Sleep(TimeSpan.FromSeconds(seconds));
Console.WriteLine("Work is done!");
workEvent.Set();//将事件设为终止状态允许一个或多个线程继续
Console.WriteLine("Waiting for a main thread to complete its work"); mainEvent.WaitOne();//阻止当前线程,直到mainEvent收到信号
Console.WriteLine("Starting second operation...");
Thread.Sleep(TimeSpan.FromSeconds(seconds));
Console.WriteLine("Work is done");
workEvent.Set();
}
}
}

注释:AutoResetEvent采用的是内核模式,所以等待时间不能太长

2.6 使用ManualResetEventSlim类

using System;
using System.Threading; namespace MulityThreadNote
{
class Program
{
static void Main(string[] args)
{
Thread t1 = new Thread(() => TravelThroughGates("Thread 1", ));
Thread t2 = new Thread(() => TravelThroughGates("Thread 2", ));
Thread t3 = new Thread(() => TravelThroughGates("Thread 3", ));
t1.Start();
t2.Start();
t3.Start();
Thread.Sleep(TimeSpan.FromSeconds());
Console.WriteLine("The gates are now open");
mainEvent.Set();//将时间设置为有信号,从而让一个或多个等待该事件的线程继续
Thread.Sleep(TimeSpan.FromSeconds());
mainEvent.Reset();//将事件设置为非终止,从而导致线程受阻
Console.WriteLine("The gates have been closed!");
Thread.Sleep(TimeSpan.FromSeconds());
Console.WriteLine("The gates are now open for the second time");
mainEvent.Set();
Thread.Sleep(TimeSpan.FromSeconds());
Console.WriteLine("The gates have been closed!");
mainEvent.Reset();
Console.ReadLine();
}
static ManualResetEventSlim mainEvent = new ManualResetEventSlim(false); static void TravelThroughGates(string threadName, int seconds)
{
Console.WriteLine($"{threadName} falls to sleep");
Thread.Sleep(TimeSpan.FromSeconds(seconds));
Console.WriteLine($"{threadName} waits for the gates to open!");
mainEvent.Wait();//阻止当前线程
Console.WriteLine($"{threadName} enter the gates!");
}
}
}

注释:ManualResetEventSlim工作方式像人群通过的大门,一直保持大门敞开直到调用reset,set相当于打开大门,reset相当于关闭大门

2.7 使用CountDownEvent类

using System;
using System.Threading; namespace MulityThreadNote
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Starting two operations");
Thread t1 = new Thread(() => PerformOperation("Operation 1 is completed", ));
Thread t2 = new Thread(() => PerformOperation("Operation 2 is completed", ));
t1.Start();
t2.Start();
//开启了两个线程,调用Wait方法阻止当前线程,知道所有线程都完成
countdown.Wait();
Console.WriteLine("Both operations have been completed");
countdown.Dispose();
Console.ReadLine();
}
//计数器初始化CountdownEvent实例,计数器表示:当计数器个数完成操作发出信号
static CountdownEvent countdown = new CountdownEvent(); static void PerformOperation(string message, int seconds)
{
Thread.Sleep(TimeSpan.FromSeconds(seconds));
Console.WriteLine(message);
//向CountdownEvent注册信息,并减少当前计数器数值
countdown.Signal();
}
}
}

注释:如果Signal方法没有达到指定的次数,那么countdown.wait()会一直等待,所以请确保所有线程完成后都要调用Signal方法

2.8 使用Barrier类

using System;
using System.Threading; namespace MulityThreadNote
{
class Program
{
static void Main(string[] args)
{
Thread t1 = new Thread(() => PlayMusic("the gutarist", "play an amazing solo", ));
Thread t2 = new Thread(() => PlayMusic("the signer", "sing his song", ));
t1.Start();
t2.Start();
Console.ReadLine();
}
//后面的Lamda表达式是回调函数。执行完SignalAndWait后执行
static Barrier barrier = new Barrier(, b=>Console.WriteLine($"End of phase {b.CurrentPhaseNumber + 1}")); static void PlayMusic(string name, string message, int seconds)
{
for (int i = ; i < ; i++)
{
Console.WriteLine("===========================");
Thread.Sleep(TimeSpan.FromSeconds(seconds));
Console.WriteLine($"{name} starts to {message}");
Thread.Sleep(TimeSpan.FromSeconds(seconds));
Console.WriteLine($"{name} finishes to {message}");
//等所有调用线程都结束
barrier.SignalAndWait();
}
}
}
}

注释:

2.9 使用ReaderWriterlockSlim类

using System;
using System.Collections.Generic;
using System.Threading; namespace MulityThreadNote
{
class Program
{
static void Main(string[] args)
{
new Thread(Read) { IsBackground = true }.Start();
new Thread(Read) { IsBackground = true }.Start();
new Thread(Read) { IsBackground = true }.Start();
new Thread(() => Write("Thread 1")) { IsBackground = true }.Start();
new Thread(() => Write("Thread 2")) { IsBackground = true }.Start();
Thread.Sleep(TimeSpan.FromSeconds());
Console.ReadLine();
}
//实现线程安全
static ReaderWriterLockSlim _rw = new ReaderWriterLockSlim();
static Dictionary<int, int> items = new Dictionary<int, int>(); static void Read()
{
Console.WriteLine("Reading contents of a dictionary");
while (true)
{
try
{
//读锁
_rw.EnterReadLock();
foreach (var key in items.Keys)
{
Thread.Sleep(TimeSpan.FromSeconds(0.1));
}
}
finally
{
//计数为0时退出读取模式
_rw.ExitReadLock();
}
}
} static void Write(string threadName)
{
while (true)
{
try
{
int newKey = new Random().Next();
_rw.EnterUpgradeableReadLock();
if (!items.ContainsKey(newKey))
{
try
{
//写锁
_rw.EnterWriteLock();
items[newKey] = ;
Console.WriteLine($"New key {newKey} is added to a dictionary by a {threadName}");
}
finally
{
//计数为0时退出写入模式
_rw.ExitWriteLock();
}
}
Thread.Sleep(TimeSpan.FromSeconds(0.1));
}
finally
{
//计数为0时退出可升级模式
_rw.ExitUpgradeableReadLock();
}
}
}
}
}

注释:从集合读取数据时,根据当前数据决定是否获取一个写锁并修改该集合。获取写锁后集合会处于阻塞状态。

2.10 使用SpinWait类

using System;
using System.Collections.Generic;
using System.Threading; namespace MulityThreadNote
{
class Program
{
static void Main(string[] args)
{
Thread t1 = new Thread(UserModeWait);
Thread t2 = new Thread(HybridSpinWait);
Console.WriteLine("Running user mode waiting");
t1.Start();
Thread.Sleep();
_isComplete = true;
Thread.Sleep(TimeSpan.FromSeconds());
_isComplete = false;
Console.WriteLine("Running hybrid SpinWait construct waiting");
t2.Start();
Thread.Sleep();
_isComplete = true;
Console.ReadLine();
}
//volatile 一个字段可能会被多个线程同时修改,不会被编译器和处理器优化为只能被单个线程访问
static volatile bool _isComplete = false; static void UserModeWait()
{
while (!_isComplete)
{
Console.WriteLine(".");
}
Console.WriteLine();
Console.WriteLine("Waiting is complete");
} static void HybridSpinWait()
{
var w = new SpinWait();
while (!_isComplete)
{
//执行单一自旋
w.SpinOnce();
//NextSpinWillYield:获取对SpinOnce的下一次调用是否将产生处理,同时触发强制上下文切换
//显示线程是否切换为阻塞状态
Console.WriteLine(w.NextSpinWillYield);
}
Console.WriteLine("Waiting is complete");
}
}
}

注释:

C#多线程编程实战(二):线程同步的更多相关文章

  1. Java多线程编程实战指南(核心篇)读书笔记(二)

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/76651408冷血之心的博客) 博主准备恶补一番Java高并发编程相 ...

  2. C#多线程编程实战(二)

    1.1 简介 为了防止一个应用程序控制CPU而导致其他应用程序和操作系统本身永远被挂起这一可能情况,操作系统不得不使用某种方式将物理计算分割为一些虚拟的进程,并给予每个执行程序一定量的计算能力.此外操 ...

  3. Java多线程编程实战指南(核心篇)读书笔记(五)

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/76730459冷血之心的博客) 博主准备恶补一番Java高并发编程相 ...

  4. Java多线程编程实战指南(核心篇)读书笔记(三)

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/76686044冷血之心的博客) 博主准备恶补一番Java高并发编程相 ...

  5. 《Java多线程编程实战指南(核心篇)》阅读笔记

    <Java多线程编程实战指南(核心篇)>阅读笔记 */--> <Java多线程编程实战指南(核心篇)>阅读笔记 Table of Contents 1. 线程概念 1.1 ...

  6. Java多线程编程实战指南(核心篇)读书笔记(四)

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/76690961冷血之心的博客) 博主准备恶补一番Java高并发编程相 ...

  7. Java多线程编程实战指南(核心篇)读书笔记(一)

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/76422930冷血之心的博客) 博主准备恶补一番Java高并发编程相 ...

  8. ASP.Net教程系列:多线程编程实战(一)

    Web开发中使用多线程可以增强用户体验,尤其是多用户.多任务.海量数据和资源紧张的情况下.所以我们的ASP.Net教程设立多线程编程实战专题.下面这些代码范例都是入门级的,希望对对大家学习ASP.Ne ...

  9. Java多线程编程实战02:多线程编程模型

    多线程编程模型 线程安全名词 串行.并发和并行 串行:一个人,将任务一个一个完成 并发:一个人,有策略地同时做多件事情 并行:多个人,每人做一个事情 竞态 名词 竞态:计算结果的正确性与时间有关的现象 ...

  10. Java多线程编程实战读书笔记(一)

    多线程的基础概念本人在学习多线程的时候发现一本书——java多线程编程实战指南.整理了一下书中的概念制作成了思维导图的形式.按照书中的章节整理,并添加一些个人的理解.

随机推荐

  1. bootstrap-table插件数据加载方式

    data-url 直接使用data-url在table标签中定义 使用load方法加载数据 $(“#finishingTask”).bootstrapTable(‘load’,data); //dat ...

  2. Ubuntu14.04+caffe+CPU

    刚刚在上篇博客记录了windows10下GPU版本caffe的安装,正准备跑跑论文里的代码,发现好多命令都是.sh命令,这是linux系统的脚本文件.不能直接在windows下运行,于是我想把.sh转 ...

  3. 同步阿里云镜像到本地,在本地搭建YUM仓库

    1.下载阿里云镜像repo文件 项目使用CentOS6系统,因此我下载的文件是: # CentOS-Base.repo # # The mirror system uses the connectin ...

  4. UML和模式应用5:细化阶段(1)--第1次迭代

    1.前言 从本文开始进入细化阶段,讨论迭代技术的基础,本次讨论将着重讨论第一次迭代,以POS机为例. 2. 第一次迭代处理的需求(以NextGen POS项目处理销售用例) 实现 处理销售 用例中基本 ...

  5. mysql之 innobackupex备份+binlog日志的完全恢复【转】

    前言: MySQL的完全恢复,我们可以借助于完整的 备份+binlog 来将数据库恢复到故障点. 备份可以是热备与逻辑备份(mysqldump),只要备份与binlog是完整的,都可以实现完全恢复. ...

  6. 性能测试十一:jmeter进阶之java请求

    使用Java编写JDBC脚本对Mysql进行增删改查等操作的性能测试 使用Jmeter提供的脚本框架依赖的jar包(分别在jmeter目录下的lib和ext目录下) ApacheJMeter_core ...

  7. python 全栈开发,Day63(子查询,MySQl创建用户和授权,可视化工具Navicat的使用,pymysql模块的使用)

    昨日内容回顾 外键的变种三种关系: 多对一: 左表的多 对右表一 成立 左边的一 对右表多 不成立 foreign key(从表的id) refreences 主表的(id) 多对多 建立第三张表(f ...

  8. 《剑指offer》-铺地砖方案数

    我们可以用21的小矩形横着或者竖着去覆盖更大的矩形.请问用n个21的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法? 又是斐波那契...稍微变形一下. class Solution { pub ...

  9. OpenLDAP在win2008上安装配置

    业务需要,将企业员工信息统一管理,OpenLdap是目录数据库和一套访问协议组成的系统,适合使用,它有以下特点: LDAP的结构用树来表示 C/S模型,Server用户存储数据,Client为操作目录 ...

  10. .NetCore源码阅读笔记系列之HttpAbstractions(五) Authentication

    说道认证&授权其实这块才是核心,这款跟前面Security这块有者紧密的联系,当然 HttpAbstractions 不光是认证.授权.还包含其他Http服务和中间价 接下来先就认证这块结合前 ...