Threads Cannot be Implemented as a Library.ppt
《Threads Cannot be Implemented as a Library.ppt》由会员分享,可在线阅读,更多相关《Threads Cannot be Implemented as a Library.ppt(30页珍藏版)》请在麦多课文档分享上搜索。
1、Threads Cannot be Implemented as a Library,Hans-J. Boehm,About the Author,Hans-J. Boehm Boehm conservative garbage collector Parallel GC for C/C+ Participated in revising the Java Memory Model Co-authored the Memory model for multi-threaded C+ Compiler-centric background,Introduction,Multi-threaded
2、programs are ubiquitous Many programs need to manage logically concurrent interactions Multiprocessors are becoming mainstream Desktop computers support multiple hardware contexts, which makes them logically multiprocessors Multi-threaded programs are a good way to utilize increasing hardware parall
3、elism,Thread support,Threads included in language specification Java C# Ada Multiple-threads not a part of language specification C/C+ Thread support provided by add-on libraries Posix threads Ptreads standard does not specify formal semantics for concurrency,Memory Model,Which assignments to a vari
4、able by one thread can be seen by a concurrently executing thread Sequential Consistency All actions occur in a total order (the execution order) that is consistent with program order; furthermore, each read r of a variable v sees the value written by the write w to v such that: w comes before r in
5、the execution order, and There is no other write w such that w comes before w and w comes before r in the execution order Happens-Before Simple version of java memory model, slightly too weak Weak Allows for compiler optimizations,Surprising results caused by statement reordering,r1 & r2 are local,
6、A & B are shared Write in one thread Read of same variable in another thread Write and read are not ordered by synchronization -,Surprising results caused by statement reordering,r1 & r2 are local, A & B are shared Write in one thread Read of same variable in another thread Write and read are not or
7、dered by synchronization Race Condition!,Pthread approach,Provided as add-on library Include hardware instructions to prevent reordering Avoid compiler reordering by appearing as an opaque function Require disciplined style of synchronization Valid 98% of the time What about the other two percent?,P
8、thread correctness,Apparently correct programs may fail intermittently New compiler or hardware induced failure Poor performance may force slight rule bending Difficult for programmer to reason about correctness Lets see some examples why,Concurrent modification,Pthread specifications prohibit races
9、 But is this enough? x=y=0 if(x=1) +y; +y; if(x!=1) -y; if (y=1) +x; +x; if (y!=1) -x; Is x=1 y=1 acceptable? No for sequential consistent interpretation But, if the compiler makes the modifications on the right, there is a race!,T1:,T2:,Why threads cannot be implemented as a library,Argument ( 1 )
10、Since the compiler is unaware of threads, it is allowed to transform code subject only to sequential correctness constraints and produce a raceBut, example is kind of far-fetched,Rewriting of Adjacent Data,Bit fields on a little endian 32-bit machine Concurrent write to memory location, not variable
11、.,Implementation of x.a=42 tmp = x; tmp /replace x ,struct int a:17; int b:15 x;,Rewriting of Adjacent Data,Bit fields on a little endian 32-bit machine Concurrent write to memory location, not variable.,Implementation of x.a=42 tmp = x; tmp /replace x ,struct int a:17; int b:15 x;,Updates to x.b in
12、troduce a race,Why threads cannot be implemented as a library,Argument ( 2 )For languages like C, if the specification does not define when adjacent data can be overwritten, then race conditions can be introduced. If so, then the compiler would know to avoid this optimization,Register promotion,for(
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- THREADSCANNOTBEIMPLEMENTEDASALIBRARYPPT
