临界区互斥
一、基本概念
1.1 临界概念
定义:
- 临界资源:一次仅允许1个进程/线程使用的资源。比如,进程间的共享内存
- 临界区:对临界资源进行访问的代码片段。比如,进程中对共享内存进行访问的程序片段
中介者模式(Mediator Design Pattern):定义一个单独的(中介)对象,来封装一系列对象的交互。将这组对象之间的交互都委派给中介对象,来避免对象之间的直接交互。
英文原文:
Mediator pattern defines a separate (mediator) object that encapsulates the interaction between a set of objects and the objects delegate their interaction to a mediator object instead of interacting with each other directly.
它有几个特点:
中介者模式,也称为调停模式,负责多个对象之间的互相沟通。
职责链模式(Chain of Responsibility Design Pattern):将请求的发送和接收解耦,让多个接收者都有机会处理这个请求。将接收者串联起来,并沿着接收者链传递请求,直到某个接收者能够处理它为止。
英语原文:
Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it.
职责链的几个特点:
职责链的目的在于,找到一个可以处理请求的接收者就好了。
策略模式(Strategy Design Pattern):定义一系列算法,并将算法封装起来,使得它们可以相互替换。策略模式可以使得算法独立于客户端(即使用算法的地方)。
英语原文:
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
策略模式有几个特点:
比如说,排序功能,排序的结果可能都是一样的,但是可以使用不同的算法,如选择排序、冒泡排序、快速排序等。