【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:
  1. Multiprogramming: 单个处理器交错运行任务the processes multiplex their execution on a single processor.
  2. Multiprocessing:多个处理器共享内存分别执行任务 the processes multiplex their execution on tightly coupled processors (processors sharing memory).
  3. 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:

  1. Load the value of x from memory into some register.
  2. Manipulate the value in the register (here, increment or decrement by one).
  3. 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:

  1. Acquire: thread calls this to obtain the lock or waits until it is free.
  2. 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只有收到这个信号才能开始执行

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值