Threads cannot be implemented as a library Hans-J. Boehm (presented by Max W Schwarz)
-
Upload
benjamin-merritt -
Category
Documents
-
view
213 -
download
0
Transcript of Threads cannot be implemented as a library Hans-J. Boehm (presented by Max W Schwarz)
![Page 1: Threads cannot be implemented as a library Hans-J. Boehm (presented by Max W Schwarz)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bf8a1a28abf838c8a5aa/html5/thumbnails/1.jpg)
Threads cannot be implemented as a library
Hans-J. Boehm(presented by Max W Schwarz)
![Page 2: Threads cannot be implemented as a library Hans-J. Boehm (presented by Max W Schwarz)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bf8a1a28abf838c8a5aa/html5/thumbnails/2.jpg)
Concurrency as a Library
● Many common languages are designed without native concurrency support.
● Consequently, many of these languages have libraries that provide locking primitives.
● This mostly works, but there are some unpleasant surprises that are non-obvious.
![Page 3: Threads cannot be implemented as a library Hans-J. Boehm (presented by Max W Schwarz)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bf8a1a28abf838c8a5aa/html5/thumbnails/3.jpg)
Fundamental Problems
● Many of the problems with multiprocessors lie at the hardware or compiler levels, and thus cannot be addressed with libraries.
● This requires a good understanding of underlying implementation to do this correctly.
![Page 4: Threads cannot be implemented as a library Hans-J. Boehm (presented by Max W Schwarz)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bf8a1a28abf838c8a5aa/html5/thumbnails/4.jpg)
Overview of sequential consistency
● View concurrent execution as an “interleaving of the steps from the threads.”
x = 1; r1 = y;y = 1; r2 = x;● One or both of r1 and r2 must have the value
1.
![Page 5: Threads cannot be implemented as a library Hans-J. Boehm (presented by Max W Schwarz)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bf8a1a28abf838c8a5aa/html5/thumbnails/5.jpg)
The real world
● This model is inefficient and disallows many types of optimizations.
● Nearly all modern architectures (like x86) allow the hardware to reorder memory operations.
● Compilers will generally reorder memory loads and stores for efficiency.
![Page 6: Threads cannot be implemented as a library Hans-J. Boehm (presented by Max W Schwarz)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bf8a1a28abf838c8a5aa/html5/thumbnails/6.jpg)
The Pthreads way
● Pthreads uses functions that guaranteed to “synchronize memory”. These include hardware instructions to prevent memory reordering.
● pthread_mutex_lock() and others● The compiler treats them as black boxes
which may touch global memory, so it won’t move memory across a function call.
![Page 7: Threads cannot be implemented as a library Hans-J. Boehm (presented by Max W Schwarz)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bf8a1a28abf838c8a5aa/html5/thumbnails/7.jpg)
Surprises
● This approach is not precise enough.● Programs may fail intermittently, or when
using a new compiler or hardware.● Thread ordering is often non-deterministic,
making this trickier to debug.● In some cases, it excludes efficient
algorithms
![Page 8: Threads cannot be implemented as a library Hans-J. Boehm (presented by Max W Schwarz)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bf8a1a28abf838c8a5aa/html5/thumbnails/8.jpg)
Consider this example
if (x == 1) ++y;if (y == 1) ++x;
![Page 9: Threads cannot be implemented as a library Hans-J. Boehm (presented by Max W Schwarz)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bf8a1a28abf838c8a5aa/html5/thumbnails/9.jpg)
Consider this example
Becomes++y; if (x != 1) --y;++x; if (y != 1) --x;Outcome of the later is not consistent.
![Page 10: Threads cannot be implemented as a library Hans-J. Boehm (presented by Max W Schwarz)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bf8a1a28abf838c8a5aa/html5/thumbnails/10.jpg)
Adjacent data
struct { int a:17; int b:15 } x;becomes{
tmp = x; // Read both fields into// 32-bit variable.tmp &= ~0x1ffff; // Mask off old a.tmp |= 42;x = tmp; // Overwrite all of x.}
![Page 11: Threads cannot be implemented as a library Hans-J. Boehm (presented by Max W Schwarz)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bf8a1a28abf838c8a5aa/html5/thumbnails/11.jpg)
Adjacent data
struct { char a; char b; char c; char d; char e; char f; char g; char h; } x;Let’s say you wish to assign each variable independently:x.b = ’b’; x.c = ’c’; x.d = ’d’; x.e = ’e’; x.f = ’f’; x.g = ’g’; x.h = ’h’;
![Page 12: Threads cannot be implemented as a library Hans-J. Boehm (presented by Max W Schwarz)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bf8a1a28abf838c8a5aa/html5/thumbnails/12.jpg)
Adjacent data
Instead of assigning each variable independently, the compiler may compress that to.x = ’hgfedcb\0’ | x.a;Pthreads allows this, but this may lead to non-portable code. Why?
![Page 13: Threads cannot be implemented as a library Hans-J. Boehm (presented by Max W Schwarz)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bf8a1a28abf838c8a5aa/html5/thumbnails/13.jpg)
Register promotion
● It is unsafe to promote variables in a critical section to places outside the critical section.
![Page 14: Threads cannot be implemented as a library Hans-J. Boehm (presented by Max W Schwarz)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bf8a1a28abf838c8a5aa/html5/thumbnails/14.jpg)
Register Promotion Example
for (...) {...if (mt) pthread_mutex_lock(...);x = ... x ...if (mt) pthread_mutex_unlock(...);}
This gets transformed to
![Page 15: Threads cannot be implemented as a library Hans-J. Boehm (presented by Max W Schwarz)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bf8a1a28abf838c8a5aa/html5/thumbnails/15.jpg)
Register Promotion Exampler = x;for (...) {
...if (mt) {
x = r; pthread_mutex_lock(...); r = x;}r = ... r ...if (mt) {
x = r; pthread_mutex_unlock(...); r = x;}
}x = r;
![Page 16: Threads cannot be implemented as a library Hans-J. Boehm (presented by Max W Schwarz)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bf8a1a28abf838c8a5aa/html5/thumbnails/16.jpg)
Performance Issues
● Pthreads assumes that memory operation order is irrelevant unless explicitly specified by the coder.
● This strategy precludes lock-free and wait-free strategies.
● The performance of these strategies is lost when reordering is restricted.
![Page 17: Threads cannot be implemented as a library Hans-J. Boehm (presented by Max W Schwarz)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bf8a1a28abf838c8a5aa/html5/thumbnails/17.jpg)