(C#) Lock - 将对象上锁,互斥多个线程,使同步。
C# Lock
原文:http://www.dotnetperls.com/lock
Locking is essential in threaded programs. It restricts code from being executed by more than one thread at the same time. This makes threaded programs reliable. The lockstatement uses a special syntax form to restrict concurrent access.
Note:Lock is compiled into a lower-level implementation based on threading primitives.
Example
Here we see a static method "A" that uses the lock statement on an object. When the method A is called many times on new threads, each invocation of the method accesses the threading primitives implemented by the lock.
Then:Only one method A can call the statements protected by the lock at a single time, regardless of the thread count.
Program that uses lock statement: C# using System;
using System.Threading; class Program
{
static readonly object _object = new object(); static void A()
{
// Lock on the readonly object.
// ... Inside the lock, sleep for 100 milliseconds.
// ... This is thread serialization.
lock (_object)
{
Thread.Sleep(100);
Console.WriteLine(Environment.TickCount);
}
} static void Main()
{
// Create ten new threads.
for (int i = 0; i < 10; i++)
{
ThreadStart start = new ThreadStart(A);
new Thread(start).Start();
}
}
} Possible output of the program 28106840
28106949
28107043
28107136
28107246
28107339
28107448
28107542
28107636
28107745
In this example, the Main method creates ten new threads, and then calls Start on each one. The method A is invoked ten times, but the tick count shows the protected method region is executed sequentially—about 100 milliseconds apart.
Note:If you remove the lock statement, the methods will be executed all at once, with no synchronization.
Intermediate representation
Let's examine the intermediate representation for the lock statement in the above example method A. In compiler theory, high-level source texts are translated to lower-level streams of instructions.
Tip:The lock statement here is transformed into calls to the static methods Monitor.Enter and Monitor.Exit.
Also:The lock is actually implemented with a try-finally construct. This uses the exception handling control flow.
Intermediate representation for method using lock .method private hidebysig static void A() cil managed
{
.maxstack 2
.locals init (
[0] object obj2)
L_0000: ldsfld object Program::_object
L_0005: dup
L_0006: stloc.0
L_0007: call void [mscorlib]System.Threading.Monitor::Enter(object)
L_000c: ldc.i4.s 100
L_000e: call void [mscorlib]System.Threading.Thread::Sleep(int32)
L_0013: call int32 [mscorlib]System.Environment::get_TickCount()
L_0018: call void [mscorlib]System.Console::WriteLine(int32)
L_001d: leave.s L_0026
L_001f: ldloc.0
L_0020: call void [mscorlib]System.Threading.Monitor::Exit(object)
L_0025: endfinally
L_0026: ret
.try L_000c to L_001f finally handler L_001f to L_0026
}
Relativity
By using the lock statement to synchronize accesses, we are creating a communication between time and state. The state is connected to the concept of time and sequential accesses to the lock.
In the Theory of Relativity, there is also a communication between time and state. This is the speed of light, which is a constant based on the relation of time and space. This connection is present also in locks—in threading constructs.
Tip:For a better description of how relativity mirrors concurrent synchronization, please see the wizard book.
Summary
We examined the lock statement in the C# language, first seeing its usage in an example program, and then describing this synchronization. Next, we stepped into the intermediate representation and its meaning in compiler theory.
Finally:We related the Theory of Relativity and the complexities of the physical universe to the lock statement.
(C#) Lock - 将对象上锁,互斥多个线程,使同步。的更多相关文章
- Linux的线程同步对象:互斥量Mutex,读写锁,条件变量
进程是Linux资源分配的对象,Linux会为进程分配虚拟内存(4G)和文件句柄等 资源,是一个静态的概念.线程是CPU调度的对象,是一个动态的概念.一个进程之中至少包含有一个或者多个线程.这 ...
- python并发编程-进程间通信-Queue队列使用-生产者消费者模型-线程理论-创建及对象属性方法-线程互斥锁-守护线程-02
目录 进程补充 进程通信前言 Queue队列的基本使用 通过Queue队列实现进程间通信(IPC机制) 生产者消费者模型 以做包子买包子为例实现当包子卖完了停止消费行为 线程 什么是线程 为什么要有线 ...
- 死锁、Lock锁、等待唤醒机制、线程组、线程池、定时器、单例设计模式_DAY24
1:线程(理解) (1)死锁 概念: 同步中,多个线程使用多把锁之间存在等待的现象. 原因分析: a.线程1将锁1锁住,线程2将锁2锁住,而线程1要继续执行锁2中的代码,线程2要继续执行锁1中的代码, ...
- Java并发编程,互斥同步和线程之间的协作
互斥同步和线程之间的协作 互斥同步 Java 提供了两种锁机制来控制多个线程对共享资源的互斥访问,第一个是 JVM 实现的 synchronized,而另一个是 JDK 实现的 ReentrantLo ...
- GIL互斥锁与线程
GIL互斥锁与线程 GIL互斥锁验证是否存在 """ 昨天我们买票的程序发现很多个线程可能会取到同一个值进行剪除,证明了数据是并发的,但是我们为了证明在Cpython中证 ...
- Linux多线程——使用互斥量同步线程
前文再续,书接上一回,在上一篇文章: Linux多线程——使用信号量同步线程中,我们留下了一个如何使用互斥量来进行线程同步的问题,本文将会给出互斥量的详细解说,并用一个互斥量解决上一篇文章中,要使用两 ...
- Linux多线程--使用互斥量同步线程【转】
本文转载自:http://blog.csdn.net/ljianhui/article/details/10875883 前文再续,书接上一回,在上一篇文章:Linux多线程——使用信号量同步线程中, ...
- WPF [调用线程无法访问此对象,因为另一个线程拥有该对象。] 解决方案以及如何实现字体颜色的渐变
本文说明WPF [调用线程无法访问此对象,因为另一个线程拥有该对象.] 解决方案以及如何实现字体颜色的渐变 先来看看C#中Timer的简单说明,你想必猜到实现需要用到Timer的相关知识了吧. C# ...
- 使用原子类或synchronized(没用Lock)解决阐述多线程所遇到线程安全问题和解决方案
例子题目: 创建10个线程,每个线程执行10000次加1,输出总和 正常结果100000 但是如果出现线程不安全会低于100000 import java.util.concurrent.Count ...
随机推荐
- HTML DOM部分---事件 windows对象;
<!--DOM操作windows对象操作 对浏览器进行操作document对象操作 对浏览器内页面文件进行操作 window.shuxing;属性调用格式window.fangfa();方法调用 ...
- alias sample method——运行时间复杂度为O(1)的抽样算法
根据离散离散概率分布抽样是一个常见的问题.这篇文章将介绍运行时间复杂度为O(1)的 alias method 抽样算法思想. 下面举例说明: 比如 a,b,c,d 的概率分别为 0.1,0.2,0.3 ...
- UVa 489 刽子手游戏
游戏规则,计算机想一个单词让你猜,你每次可以猜一个字母,如果单词里有那个字母,所有该字母都会显示出来,如果没有那个字母则计算机会在一副"刽子手"画上填一笔,这幅画一共需要7笔就能完 ...
- Apache脚本路径别名(CGI接口)
CGI:Common Gateway Interface(通用网关接口)使WEB可以跟一个应用程序进行通信,从通信环境中获得结果. CGI是不安全的,需要mod_alias,mod_cgi模块 Scr ...
- linux权限管理_ACL权限
一.什么是ACL权限 ACL是Access Control List(访问控制列表)的缩写,主要的目的是在提供传统的owner,group,others的read,write,execute权限之外的 ...
- 【NOIP2010】【P1317】乌龟棋
似乎很像搜索的DP(应该也可以用搜索写) 原题: 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物.乌龟棋的棋盘是一行N 个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N 格是终点, ...
- 《FreeSWITCH: VoIP实战》:SIP 模块 - mod_sofia
SIP 模块是 FreeSWITCH 的主要模块,所以,值得拿出专门一章来讲解. 在前几章时里,你肯定见过几次 sofia 这个词,只是或许还不知道是什么意思.是这样的,Sofia-SIP 是由诺基亚 ...
- loadrunner---<二>---菜鸟对cookie的思考
http://www.cnblogs.com/Pierre-de-Ronsard/archive/2012/11/19/2772630.html loadrunner---<二>---菜鸟 ...
- shell之eval-command
本文将会讲解一些linux中命令的使用与技巧希望对新手给予帮助一 e v a l命令将会首先扫描命令行进行所有的置换,然后再执行该命令.该命令适用于那些一次扫描无法实现其功能的变量.该命令对变量进行两 ...
- alpha预乘
将(r,g,b,a)变为(r*a,g*a,b*a,a)的操作称为alpha预乘. 对于alpha预乘的图片,应使用(One,OneMinusSrcAlpha)进行混合. 使用alpha预乘方式混合出来 ...