Skip to Content
C#并发编程经典实例(第2版)
book

C#并发编程经典实例(第2版)

by Stephen Cleary
November 2020
Intermediate to advanced
226 pages
5h 34m
Chinese
Posts & Telecom Press
Content preview from C#并发编程经典实例(第2版)
160
12
同步
当应用程序利用并发时(就像所有
.NET
应用程序实际使用的那样),如果一部分代码需要
更新数据,而其他代码需要访问相同的数据,那么需要格外当心。每当发生这种情况时,
就需要
同步访问
数据。本章会探讨同步访问中较为常用的几种类型。然而,如果合理使用
了本书介绍的其他方法,就会发现各种库其实已经处理好了多种较为常见的同步。在探索
同步方法之前,先来深入了解一些可能需要或可能不需要同步的情况吧。
本章对同步的解释略有简化,但结论全部正确。
同步的两大类型分别是
交互
数据保护
。当一段代码需要把某个情况(比如,收到了一条
新消息)通知给另一段代码时,就要使用交互。本章会对交互进行全面的探讨,现在先来
看一下数据保护。
当下列
3
个条件全部满足时,需要通过同步来保护共享数据。
多段代码并发运行。
这些代码访问(读取或写入)同样的数据。
至少有一段代码在更新(写入)数据。
1
个条件的原因显而易见,如果整段代码从始至终没有执行任何并发操作,那么完全不需
要担心同步的问题。某些简单的控制台应用程序就属于这种情况,但绝大多数的
.NET
应用
程序确实会使用某种并发。第
2
个条件表示如果每一段代码拥有自己的局部数据,且不能
同步
161
共享,那就没有同步的必要,局部数据不可能从任何其他代码段中访问。若是
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

解密金融数据

解密金融数据

Justin Pauley
PHP编程:第4版

PHP编程:第4版

Kevin Tatroe, Peter MacIntyre

Publisher Resources

ISBN: 9787115550606