版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、并发算法与理论Concurrency: Algorithms and Theories梁红瑾Programmers View2C+ Programmers View#include #include void hello() std:coutHello Concurrent Worldn;int main() std:thread t(hello); t.join(); return 0;3C+ Programmers ViewOutputs4Taking a More Abstract View Parallel compositionC1 | C25for(int i=0; i 10; i
2、+) cout“main concurrentn;for(int i=0; i 10; i+) cout“hello concurrentn;Taking a More Abstract View Shared memory & interleaving semantics6. . .MemoryT1T2T3TnTaking a More Abstract View Shared memory & interleaving semantics7for(int i=0; i 10; i+) cout“main concurrentn;for(int i=0; i 10; i+)
3、cout“hello concurrentn;Memory (e.g. cout)T1T2Java Threads8Java Threads9public class MyRunnable implements Runnable Override public void run() for(int i = 0; i 10; i+) System.out.println(i); public class MyRunnableTest public static void main(String args) MyRunnable mr1 = new MyRunnable(); MyRunnable
4、 mr2 = new MyRunnable(); Thread t1 = new Thread(mr1); Thread t2 = new Thread(mr2); t1.start(); t2.start(); Python Threads10Python Threads11Problems with Concurrency Nondeterministic! Recall: interleaving semantics12MemoryT1T2T3TnProblems with Concurrency Nondeterministic! Recall: interleaving semant
5、ics13Possible executionsProgramT1T2|= Difficult to find a bug Difficult to reproduce a bugMore C+14#include #include void thread_function() for (int i = -100; i 0; i+) std:cout thread function: i n;int main()std:thread t(thread_function);for (int i = 0; i 100; i+) std:cout main thread: i n;t.join();
6、return 0;thread function: main thread: 0main thread: 1main thread: 2main thread: 3-100thread function: -99main thread: 4main thread: 5main thread: 6Using Locks 15#include #include #include #include std:mutex mu;void shared_cout(std:string msg, int id)mu.lock();std:cout msg : id std:endl;mu.unlock();
7、Using Locks 16#include #include #include #include std:mutex mu;void shared_cout(std:string msg, int id)mu.lock();std:cout msg : id std:endl;mu.unlock();void thread_function()for (int i = -100; i 0; i-) shared_cout(main thread, i);t.join();return 0;More Locks in C+17#include #include #include #includ
8、e std:mutex mu;void shared_cout(std:string msg, int id)std:lock_guard guard(mu);std:cout msg : id std:endl;More Locks in C+18#include #include #include std:list some_list;std:mutex some_mutex;void add_to_list(int new_value) std:lock_guard guard(some_mutex); some_list.push_back(new_value);bool list_c
9、ontains(int value_to_find) std:lock_guard guard(some_mutex); return std:find(some_list.begin(),some_list.end(),value_to_find) != some_list.end();Taking a More Abstract View Synchronization operations lock/unlock, acq/rel19for (int i=-100; i0; i+) mu.lock();std:cout thread function: i 0; i-) mu.lock(
10、);std:cout main thread: i n; mu.unlock();20But waitHow are lock() and unlock() implemented?Are there interleavings between the threads when executing the implementations of lock()/unlock()?for (int i=-100; i0; i+) mu.lock();std:cout thread function: i 0; i-) mu.lock();std:cout main thread: i n; mu.u
11、nlock();Programmers View A concurrent program = concurrent objects + their clients21mu.lock();mu.unlock();mu.lock();mu.unlock();void lock() void unlock() Client codeConcurrent objectT1T2T3lock() Programmers View A concurrent program = concurrent objects + their clients22s.push(7);x = s.pop();s.push(
12、6);void push(int v) int pop() T1T2T3push() Concurrent objectClient codeConcurrent Objects23memoryobjectobjectthreadsProgrammers View Parallel composition, shared memory & interleaving semantics Locks & synchronization operations Concurrent objects and their clients24Memory Models25https:/en.
13、/wiki/Memory_model_(programming) Memory Models Interleaving semantics Sequential Consistency (SC) model 26MemoryT1T2T3Tn(Weak/Relaxed) Memory Models Java memory model C/C+11 memory model x86-TSO memory model ARMv8 memory model 2728Will the assertion fail?#include #include int x = 0;void
14、 foo() while (true) x = 1; x = 0; assert(x = 0); int main() std:thread t(foo); std:thread t2(foo); t.join(); t2.join();C+ Program 129Will the assertion fail?#include #include int x = 0;void foo() for ( int i = 0; i 10000000; i+ ) x = x + 1; int main() std:thread t1(foo); std:thread t2(foo); t1.join(
15、); t2.join(); assert(x = 20000000);C+ Program 2Course OverviewGoals of the Course Understanding concurrent programs Interleaving semantics Some feelings of weak memory models Understanding concurrent objects (algorithms) Correctness criteria Design ideas Why they are correct Some impossibility resul
16、ts31Preliminary Syllabus Weak memory models Java, C11 Locks Implemented by Read/Write Peterson Lock, Filter Lock, Lamports Bakery Lock Linearizability Synchronization Operations Practical Locks TAS Lock, Queue Locks List-Based Sets Concurrent Queues and Stacks Snapshot32References Maurice Herlihy and Nir Shavit. The Art of Multiprocessor Programming. Morgan Kaufmann. 2008 / 2012.33Lecture notes, homework and notifications will be posted on the course webpage.You can ask questions in QQ group, or email me your questions.Please pay attention t
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 芯片烧录设备工作原理及烧录技术全景解析
- T∕CCMI 37-2025 精冲零件剪切面质量
- 《数控机床加工零件》课件-任务介绍:安装壳体的槽和螺纹特征的加工
- 2025年贵州路桥集团有限公司招聘考试真题
- 2025年台州市科创投资集团有限公司招聘真题
- 2025年广西工艺美术研究院有限公司招聘真题
- 《商务数据可视化》课件-5.1 认识Power Query 查询编辑器
- 2026年安庆市税务系统事业单位人员招聘考试备考试题及答案详解
- 2026内蒙古巴彦淖尔市乌拉特中旗招聘公益性岗位人员3人考试备考题库及答案解析
- 2026海南三亚中心医院(海南省第三人民医院)考核招聘事业编制人员54人(第1号)考试模拟试题及答案解析
- 2026安徽安庆市宿松县事业单位招聘84人笔试备考试题及答案解析
- 2026黔东南公路建设养护有限公司招聘11人笔试参考题库及答案解析
- 2025版《广东省护理病历书写管理规范(试行)》
- 2026届重庆市高三二诊英语试题(含答案和音频)
- 个人职业形象塑造指导书
- 2025年专业公共营养师资格考试真题与答案解析
- 金太阳2026届高三联考313C语文试题(含答案)
- 华为内部晋升制度
- 银行保险机构消防安全管理指南(试行)原文下载
- 2026年应急演练计划
- 新版人教版九年级物理上册教案(完整版)教学设计
评论
0/150
提交评论