`
jubincn
  • 浏览: 242554 次
  • 性别: Icon_minigender_1
  • 来自: 宁波
文章分类
社区版块
存档分类
最新评论

6.087 Practical Programming in C, lec12

 
阅读更多

Multithreading and concurrency

Preliminaries: Parallel computing

• Parallelism: Multiple computations are done simultaneously.

• Instruction level (pipelining)

• Data parallelism (SIMD)

• Task parallelism (embarrassinglyparallel)

• Concurrency: Multiple computations that may be done inparallel.

• Concurrency vs. Parallelism

concurrencyparallelism是不同的,可以参考这篇英文文章。拿两个简单的计算任务T1T2为例,简单地说,concurrencyT1T2的执行顺序可以不确定,而parallelismT1T2可以同时在多个CPU上运算。

Process vs. Threads

• Process: An instance of a program that is being executed inits own address space. In POSIX systems, each process maintains itsown heap, stack, registers, file descriptors etc.

Communication:

• Shared memory

• Network

• Pipes, Queues

• Thread: A light weight process that shares its address spacewith others.In POSIX systems, each thread maintains the bareessentials: registers, stack, signals.

Communication:

• shared address space.

线程必须有自己的栈,因为线程的执行顺序不是线性的,如果用进程栈的话就会导致混乱。线程不需要自己的堆和文件描述符,我想是因为相对而言堆是静态的,即不会用堆的结构来控制程序的执行结构。文件描述符是将文件映射到进程的虚拟内存中所做的标记,因此也是全局性质的。

Multithreaded concurrency

Serial execution:

• All our programs so far has had asingle thread of execution: main thread.

• Program exits when the mainthread exits.

Multithreaded:

• Program is organized as multipleand concurrent threads of execution.

• The main thread spawns multiplethreads.

• The thread may communicate withone another.

• Advantages:

• Improves performance

• Improves responsiveness

• Improves utilization

• less overhead compared to multipleprocesses

多线程的程序最大的好处就是使不同类型的线程交叉运行,利用人类与机器的巨大速度差异,使人有一种程序并发执行的假象。这个在图形用户界面中和数据的读取中使用的最为成功,其实也就是MVC中的MV。现在想想,世界上很多东西都有一种神奇的联系,就像数学和机器,艺术和数学等。

使用进程来协作完成一件任务,虽然可以使内存获得更有效的保护(独立的虚拟内存),但同时也存在着这些缺点:1.进程之间的切换代价较高;2.操作系统可以有效控制的进程数是有限的;3.多进程共享的数据在同步代价较大。

Multithreaded programming

Even in C, multithread programming may be accomplished in severalways

• Pthreads: POSIX C library.

• OpenMP

• Intel threading building blocks

• Cilk (from CSAIL!)

• Grand central despatch

• CUDA (GPU)

• OpenCL (GPU/CPU)

学习曲线似乎是一个比一个高,我正打算学OpenMP,听说比较好上手且效率提升效果还行。

Not all code can be made parallel


右侧的循环不能并行化是因为存在数据依赖,我称之为数据递归。存在强依赖的递归程序(也就是咱们平时所说的递归程序)很难实现并行化。

Pthread

API:

Threadmanagement: creating, joining, attributes

pthread_

Mutexes:create, destroy mutexes

pthread_mutex_

Conditionvariables: create,destroy,wait,signal

pthread_cond_

Synchronization:read/write locks and barriers

pthread_rwlock_,pthread_barrier_

API:

#include<pthread.h>

gcc−Wall −O0 −o <output> file.c −pthread (no −l prefix)

Pthread主要使用锁来控制并发,跟数据库有些类似,这些锁主要有互斥锁,条件锁等。

Creating threads

int pthread_create(pthread_t∗thread, const pthread_attr_t ∗ attr, void ∗(∗start_routine)(void ∗), void ∗ arg);

• creates a new thread with theattributes specified by attr.

• Default attributes are used ifattr is NULL.

• On success, stores the thread itinto thread

• calls function start_routine(arg)on a separatethread of execution.

• returns zero on success, non-zeroon error.

void pthread_exit(void∗value_ptr);

• called implicitly when threadfunction exits.

• analogous to exit().

线程中的方法参数和返回值为什么都void类型呢?参数很耗理解,因为只有void可以方便地转换为其它类型。返回值是void,也就是没有返回值,我想是因为每个线程都是相对独立的,他们通过共享内存(堆?)来进行通信,而且其程序运行在自己的栈中,因此不需要也不能通过返回值进行通信。Thread的停止条件有这几个:1.调用pthread_exit2.start_routine返回;3.线程被取消pthread_cancel

Synchronization: joining


int pthread_join(pthread_tthread, void ∗∗value_ptr);

• pthread_join() blocks the callingthread until the specified thread terminates.

• If value_ptr is not null, it willcontain the return status of the called thread

Other ways to synchronize: mutex,condition variables

如果线程T1 join线程T2,那么T1将等待T2结束后才开始执行。线程是先创建然后再设置join,因此需要将attr设置为PTHREAD_CREATE_JOINABLE,防止线程在创建时就运行。

Mutex(互斥锁)

• Mutex (mutual exclusion) acts as a "lock" protectingaccess to the shared resource.

• Only one thread can "own" the mutex at a time.Threads must take turns to lock the mutex.

intpthread_mutex_destroy(pthread_mutex_t ∗mutex);

intpthread_mutex_init(pthread_mutex_t ∗ mutex, constpthread_mutexattr_t ∗ attr);

thread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER ;

• pthread_mutex_init() initializes a mutex. If attributes areNULL, default attributes are used.

• The macro PTHREAD_MUTEX_INITIALIZER can be used to initializestatic mutexes.

• pthread_mutex_destroy() destroys the mutex.

• Both function return return 0 on success, non zero on error

intpthread_mutex_lock(pthread_mutex_t ∗mutex);

intpthread_mutex_trylock(pthread_mutex_t∗mutex);

intpthread_mutex_unlock(pthread_mutex_t ∗mutex);

• pthread_mutex_lock() locks the given mutex. If the mutex islocked, the function is blocked until it becomes available.

• pthread_mutex_trylock() is the non-blocking version. If themutex is currently locked the call will return immediately.

• pthread_mutex_unlock() unlocks the mutex.

互斥锁感觉没什么好说的,相关的原理在数据库系统原理里面讲了很多。不知为什么,我不是很喜欢这种方法,总感觉很复杂。

Condition variables

Sometimes locking or unlocking is based on a run-time condition(examples?).Without condition variables, program would have to pollthe variable/condition continuously.

Consumer:

(a) lock mutex on global itemvariable

(b) wait for (item>0) signal fromproducer (mutex unlocked automatically).

(c) wake up when signalled (mutexlocked again automatically), unlock mutex and proceed.

Producer:

(1) produce something

(2) Lock global item variable, updateitem

(3) signal waiting (threads)

(4) unlock mutex

intpthread_cond_wait(pthread_cond_t ∗cond, pthread_mutex_t ∗mutex);

• blocks on a condition variable.

• must be called with the mutexalready locked otherwise behavior undefined.

• automatically releases mutex

• upon successful return, the mutexwill be automatically locked again.

intpthread_cond_broadcast(pthread_cond_t ∗cond);

intpthread_cond_signal(pthread_cond_t ∗cond);

• unblocks threads waiting on acondition variable.

• pthread_cond_broadcast() unlocksall threads that are waiting.

• pthread_cond_signal() unlocks oneof the threads that are waiting.

• both return 0 on success, non zerootherwise.

MFC中的文档试—视图结构很类似,也是I/O中比较常见的一种情况。感觉这个像是mutex的一种应用模式。



分享到:
评论

相关推荐

    Umich反应工程_1-25课件.zip

    lec12.ppt lec13.ppt lec14.ppt lec15.ppt lec16.ppt lec17.ppt lec18.ppt lec19.ppt lec2.ppt lec20.ppt lec21.ppt lec22.ppt lec23.ppt lec24.ppt lec25.ppt lec3.ppt lec4.ppt lec5.ppt lec6.ppt lec7.ppt lec8....

    MIT10_626S11_lec06.pdf

    In the previous lecture, we leant about impedance spectroscopy. Electrochemical impedance spectroscopy is the technique where the cell or electrode impedance is platted versus frequency. Thus, the ...

    programming in computing 10a lec2 ppt

    programming in computing 10a lec2

    lec.rar_LEC

    6. **划分风险等级**:根据LEC分值将危险源分为不同的风险等级,如低风险、中风险和高风险。 总的来说,"lec.rar_LEC"提供的工具是进行LEC风险评估的重要助手,它使用MATLAB语言编写,能够客观地计算和分析工作场所...

    EI374 高级算法-全套 PPT 课件-笔记

    EI374 高级算法-全套 PPT 课件-笔记 lec1-slides.pdf lec1.pdf lec2-slides.pdf lec2.pdf lec3-slides.pdf lec3.pdf lec4-slides.pdf ...lec6.pdf lec7.pdf lec8.pdf lec9.pdf lec10.pdf lec11.pdf

    数字逻辑设计及应用教学英文课件:Lec12-Chap 6.ppt

    数字逻辑设计及应用教学英文课件:Lec12-Chap 6.ppt

    MIT计算机图形学课程6.837课件

    Lec 12 Ray Casting II Lec 13 Ray Tracing Lec 14 Acceleration Structures for Ray Casting Assignment 3 Lec 15 Shading and Material Appearance Lec 16 Texture Mapping and Shaders Lec 17 Sampling, ...

    EI338 计算机系统工程-Computer Systems Engineering-全套 PPT 课件

    EI338 计算机系统工程-Computer Systems Engineering-全套 PPT 课件 CA-lec1.pdf ...lec6-OS.pdf lec7-OS.pdf lec8-OS.pdf lec9-OS.pdf lec10-OS.pdf lec11-OS.pdf lec12-OS.pdf Study-Guide.pdf Summary.pdf

    lec-培训(完整版).pdf

    《LEC培训(完整版).pdf》是一份关于逻辑等效检查的详细教程,重点介绍了使用Conformal工具进行逻辑等效验证的方法和技术。Conformal是一款强大的逻辑等效检查工具,广泛应用于芯片设计的验证阶段,确保设计的逻辑...

    lec.rar_V2

    在Linux系统中,头文件是C语言编程的关键部分,它们允许程序员在源代码(如lec.c)中使用特定的函数和数据类型,而无需在每个文件中包含完整的实现。lec.c很可能是这个LAN Emulation客户端的实现文件,包含实际的...

    麻省理工matlab课件-MIT6_094IAP10_lec04.pdf

    麻省理工matlab课件-MIT6_094IAP10_lec04.pdf 本帖最后由 sunchy11 于 2012-2-8 15:46 编辑 分享个MIT的matlab 教程,属于初级入门,希望对大家有帮助哈。

    HOLLiAS-LEC G3 PLC选型手册.pdf

    由于提供的文件内容主要是一些网站链接、电子邮箱地址和数字的排列,没有提供实际的HOLLiAS-LEC G3 PLC选型手册的详细内容,我无法直接从中提取相关的知识点。然而,我可以根据HOLLiAS-LEC G3 PLC这个主题,根据一般...

    Week1—4_Note_Lec1—6.pdf

    Week1—4_Note_Lec1—6.pdf

    lec.zip_LEC _点名_点名系统_点名系统C_随机点名

    总结而言,“lec.zip_LEC _点名_点名系统_点名系统C_随机点名”提供的是一款基于C语言实现的随机点名系统,它集成了随机数生成和数据管理技术,为教学环境带来了便捷和公平。通过理解和掌握这样的系统,不仅可以提升...

    立华 LEC-3210 19’@2U嵌入式通讯管理机 产品介绍.pdf

    立华LEC-3210是一款19英寸标准2U机架式的嵌入式通讯管理机,针对电力自动化行业设计,具备无风扇和低功耗的特点。该设备基于Intel Atom D525双核处理器,搭载6个千兆以太网接口(GbE)、10/18个串行通信接口(COM)...

    Lec-1-SDLC.rar_LEC

    "Lec 1 SDLC.pptx"这个文件很可能包含了对SDLC和UML的详细介绍,涵盖了以上各个阶段的理论知识和实际应用案例。通过学习这个讲座材料,可以深入理解这两个概念如何在实际项目中协同工作,提升软件开发的效率和质量。...

    demo_Lec.m

    demo_Lec.m

    立华 LEC-2055 桌面型嵌入式工业控制器 产品介绍.pdf

    LEC-2055桌面型嵌入式工业控制器是一款专为工业应用设计的嵌入式计算机,具有小巧无风扇的特点,适用于多种工业环境。以下是LEC-2055的主要技术规格和功能特点: 1. CPU与性能 LEC-2055采用Intel Atom N270处理器,...

    Lec1-Introduction.pdf.zip.zip

    Lec1-Introduction.pdf.zip

Global site tag (gtag.js) - Google Analytics