【RTS笔记2】Concurrent Programming
Concurrent Programming:
Define
Concurrent programming techniques are those used to deal with parallelism in computing tasks. The correct logical behaviour of a concurrent program depends on correctly coordinating tasks to meet the program’s objectives.
Concurrent 需要关注两点:
- Synchronisation 同步:to satisfy constraints on the ordering of tasks.
- Communication 沟通: for passing information from one task to another
Concurrency is not parallelism 并发不是平行!
Concurrent execution can take three different forms:
- Multiprogramming: 单个处理器交错运行任务the processes multiplex their execution on a single processor.
- Multiprocessing:多个处理器共享内存分别执行任务 the processes multiplex their execution on tightly coupled processors (processors sharing memory).
- Distributed processing:多个处理器不共享内存分别执行任务 the processes multiplex their execution on loosely coupled processors (not sharing memory).
- True parallelism only occurs in 2. and 3.
“sequential” and “concurrent” 顺序与并发
- A sequential program is executed one instruction after the next (i.e., with no overlap) in the order defined in the code.
- A concurrent program contains separate, sequential, threads of execution. Instructions in different threads can potentially be executed simultaneously.
harder to debug
因为代码执行的顺序不同,因此可能每次生成的数值都不一样(bug都不一样,难以定位)
Processes 线程
Single and Multithreaded Processes 单/多线程
编一个游戏,整个程序是Process,Process里有很多thread(比如游戏源码里的代码块就可以是thread)
引用知乎上的比喻:Processes进程=火车,threads线程=车厢
Internal Process States 内部进程状态
- Waiting等待: Task is waiting for a condition (e.g., it has requested a resource not yet available, or is a periodic task awaiting a certain time, or is a sporadic task awaiting an event).
- Ready 准备: Task can be executed (i.e., it is in the queue to run).
- Running 运行: Task is currently being executed by the CPU.
- Dormant 休眠: Task has finished or not yet started.
Shared Resources 资源共享
Synchronisation 同步
多个threads 运行容易,但thread与thread之间需要分享数据来合作
Synchronisation 能保证数据完整
Conditional synchronisation条件同步:taskA先拿资源,taskB等A拿完,再拿资源
Access to shared resources访问共享资源:好多task同时分享资源
需要注意:当几个threads同时访问资源时,同时修改/读取,会出现不可预料的问题。(举个例子,某一对主副银行卡有100元,两张卡两人同时各取100元会发生啥?)为了规避这些问题,可以使用 locks, semaphores, monitors…
Race Condition
In order to update the shared variable, x, each task must:
- Load the value of x from memory into some register.
- Manipulate the value in the register (here, increment or decrement by one).
- Store the value in the register back to the memory.
执行顺序不同,输出也不同
Mutual Exclusion ,Locks ,Semaphores
以下均为简介,之后几章节将会详细讨论这三者
Mutual Exclusion 进程互斥
Mutual exclusion prevents simultaneous access to a resource by more than one task. Mutual exclusion guarantees exclusive access to a common resource. 【同一时间,一任务用一资源】
Locks 互斥锁
A lock is an object in the memory that provides two methods:
- Acquire: thread calls this to obtain the lock or waits until it is free.
- Release: thread releases the lock after it finishes its execution on the shared data.
deadlock 死锁:TaskA等B用资源,结果B也在等A用资源,他们永远的互相等待下去(死机)
Semaphores 信号量
我们要让好多processes或者好多threads 运行:
如果A需要在B前面运行,那么我们就在A结束时释放一个信号,B只有收到这个信号才能开始执行