Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code!...
Transcript of Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code!...
![Page 1: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/1.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Better Code: ConcurrencySean Parent | Principal Scientist
![Page 2: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/2.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Better Code
! Regular Type ! Goal: Implement Complete and Efficient Types
! Algorithms ! Goal: No Raw Loops
! Data Structures ! Goal: No Incidental Data Structures
! Runtime Polymorphism ! Goal: No Inheritance
! Concurrency ! Goal: No Raw Synchronization Primitives
! …
2
![Page 3: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/3.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Better Code
! Regular Type ! Goal: Implement Complete and Efficient Types
! Algorithms ! Goal: No Raw Loops
! Data Structures ! Goal: No Incidental Data Structures
! Runtime Polymorphism ! Goal: No Inheritance
! Concurrency ! Goal: No Raw Synchronization Primitives
! …
2
![Page 4: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/4.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Common Themes
! Manage Relationships ! Understand the Fundamentals ! Code Simply ! Local and Equational Reasoning
3
![Page 5: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/5.jpg)
![Page 6: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/6.jpg)
![Page 7: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/7.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Concurrency
! Concurrency: when tasks start, run, and complete in overlapping time periods ! Parallelism: when two or more tasks execute simultaneously
! Why? ! Enable performance through parallelism ! Improve interactivity by handling user actions concurrent with processing and IO
6
http://docs.oracle.com/cd/E19455-01/806-5257/6je9h032b/index.html
![Page 8: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/8.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Goal: No Raw Synchronization Primitives
7
![Page 9: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/9.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
What are raw synchronization primitives?
! Synchronization primitives are basic constructs such as: ! Mutex ! Atomic ! Semaphore ! Memory Fence ! Condition Variable
8
![Page 10: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/10.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Why No Raw Synchronization Primitives?
9
You Will Likely Get It Wrong
![Page 11: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/11.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Problems with Locks
template <typename T> class bad_cow { struct object_t { explicit object_t(const T& x) : data_m(x) {} atomic<int> count_m{1}; T data_m; }; object_t* object_m; public: explicit bad_cow(const T& x) : object_m(new object_t(x)) { } ~bad_cow() { if (0 == --object_m->count_m) delete object_m; } bad_cow(const bad_cow& x) : object_m(x.object_m) { ++object_m->count_m; }
bad_cow& operator=(const T& x) { if (object_m->count_m == 1) object_m->data_m = x; else { object_t* tmp = new object_t(x); --object_m->count_m; object_m = tmp; } return *this; } };
10
![Page 12: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/12.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Problems with Locks
template <typename T> class bad_cow { struct object_t { explicit object_t(const T& x) : data_m(x) {} atomic<int> count_m{1}; T data_m; }; object_t* object_m; public: explicit bad_cow(const T& x) : object_m(new object_t(x)) { } ~bad_cow() { if (0 == --object_m->count_m) delete object_m; } bad_cow(const bad_cow& x) : object_m(x.object_m) { ++object_m->count_m; }
bad_cow& operator=(const T& x) { if (object_m->count_m == 1) object_m->data_m = x; else { object_t* tmp = new object_t(x); --object_m->count_m; object_m = tmp; } return *this; } };
10
![Page 13: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/13.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Problems with Locks
template <typename T> class bad_cow { struct object_t { explicit object_t(const T& x) : data_m(x) {} atomic<int> count_m{1}; T data_m; }; object_t* object_m; public: explicit bad_cow(const T& x) : object_m(new object_t(x)) { } ~bad_cow() { if (0 == --object_m->count_m) delete object_m; } bad_cow(const bad_cow& x) : object_m(x.object_m) { ++object_m->count_m; }
bad_cow& operator=(const T& x) { if (object_m->count_m == 1) object_m->data_m = x; else { object_t* tmp = new object_t(x); --object_m->count_m; object_m = tmp; } return *this; } };
• There is a subtle race condition here:• if count != 1 then the bad_cow could also is owned by another
thread(s)• if the other thread(s) releases the bad_cow between these two
atomic operations• then our count will fall to zero and we will leak the object
10
![Page 14: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/14.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Problems with Locks
template <typename T> class bad_cow { struct object_t { explicit object_t(const T& x) : data_m(x) {} atomic<int> count_m{1}; T data_m; }; object_t* object_m; public: explicit bad_cow(const T& x) : object_m(new object_t(x)) { } ~bad_cow() { if (0 == --object_m->count_m) delete object_m; } bad_cow(const bad_cow& x) : object_m(x.object_m) { ++object_m->count_m; }
bad_cow& operator=(const T& x) { if (object_m->count_m == 1) object_m->data_m = x; else { object_t* tmp = new object_t(x); if (0 == --object_m->count_m) delete object_m; object_m = tmp; } return *this; } };
11
![Page 15: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/15.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Problems with Locks
12
![Page 16: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/16.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Problems with Locks
! bad_cow is not an atomic type, bad_cow<int> is as thread safe as int! --x on an atomic is equivalent to atomic_fetch_sub(x) - 1
12
![Page 17: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/17.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Problems with Locks
! bad_cow is not an atomic type, bad_cow<int> is as thread safe as int! --x on an atomic is equivalent to atomic_fetch_sub(x) - 1
! Nobody caught the bug that count_m was uninitialized
12
![Page 18: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/18.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Why do we want concurrency?
13
Performance through Parallelism
![Page 19: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/19.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved. 14
Desktop Compute Power (8-core 3.5GHz Sandy Bridge + AMD Radeon 6950)
14
![Page 20: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/20.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved. 14
Desktop Compute Power (8-core 3.5GHz Sandy Bridge + AMD Radeon 6950)
14
0 750 1500 2250 3000
GPU Vectorization Multi-thread Scalar (GFlops)
![Page 21: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/21.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved. 14
Desktop Compute Power (8-core 3.5GHz Sandy Bridge + AMD Radeon 6950)
14
OpenGL OpenCL CUDA
Direct Compute C++ AMP DirectX
0 750 1500 2250 3000
GPU Vectorization Multi-thread Scalar (GFlops)
![Page 22: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/22.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved. 14
Desktop Compute Power (8-core 3.5GHz Sandy Bridge + AMD Radeon 6950)
Intrinsics Auto-vectorization
OpenCL
14
OpenGL OpenCL CUDA
Direct Compute C++ AMP DirectX
0 750 1500 2250 3000
GPU Vectorization Multi-thread Scalar (GFlops)
![Page 23: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/23.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved. 14
Desktop Compute Power (8-core 3.5GHz Sandy Bridge + AMD Radeon 6950)
Intrinsics Auto-vectorization
OpenCL
14
TBB GCD
OpenMP C++11
OpenGL OpenCL CUDA
Direct Compute C++ AMP DirectX
0 750 1500 2250 3000
GPU Vectorization Multi-thread Scalar (GFlops)
![Page 24: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/24.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved. 14
Desktop Compute Power (8-core 3.5GHz Sandy Bridge + AMD Radeon 6950)
Intrinsics Auto-vectorization
OpenCL
14
Straight C++
TBB GCD
OpenMP C++11
OpenGL OpenCL CUDA
Direct Compute C++ AMP DirectX
0 750 1500 2250 3000
GPU Vectorization Multi-thread Scalar (GFlops)
![Page 25: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/25.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Amdahl’s Law
15
http://en.wikipedia.org/wiki/Amdahl%27s_law
![Page 26: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/26.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Amdahl’s Law
16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Processors
Performance
Each line represents 10% more synchronization
![Page 27: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/27.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Why No Raw Synchronization Primitives?
17
Object
thread
thread
thread
![Page 28: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/28.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Why No Raw Synchronization Primitives?
17
Object
thread
thread
thread
STOP
STOP
GO
![Page 29: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/29.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Why No Raw Synchronization Primitives?
17
Object
thread
thread
thread
STOP
STOP
GO
![Page 30: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/30.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Why No Raw Synchronization Primitives?
17
Object
thread
thread
thread
STOP
STOP
GO
![Page 31: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/31.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Locks
18
STOP
![Page 32: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/32.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Minimize Locks
18
STOP
![Page 33: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/33.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Mutexes and Sequential Consistency
19
class registry { mutex _mutex; unordered_map<string, string> _map; public: void set(string key, string value) { unique_lock<mutex> lock(mutex); _map.emplace(move(key), move(value)); } auto get(const string& key) -> string { unique_lock<mutex> lock(mutex); return _map.at(key); } };
![Page 34: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/34.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Mutexes and Sequential Consistency
“It can be shown that programs that correctly use mutexes and memory_order_seq_cst operations to prevent all data races and use no other synchronization operations behave as if the operations executed by their constituent threads were simply interleaved, with
each value computation of an object being taken from the last side effect on that object in that interleaving. This is normally referred to as ‘sequential consistency.’”
– C++11 Standard 1.10.21
20
![Page 35: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/35.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Mutexes and Sequential Consistency
21
.
.
.
Op4(X)
Op3(X)
Op1(X)
Op3(X)
rm
r2
r1
r0
![Page 36: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/36.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Mutexes and Sequential Consistency
! A mutex serializes a set of operations, Opn, where the operation is the code executed while the mutex is locked ! Operations are interleaved and may be executed in any order and may be repeated ! Each operation takes an argument, X, which is the set of all objects mutated under all operations ! X may not be safely read or written without holding the lock if it may be modified by a task holding the lock
! Each operation may yield a result, rm, which can communicate information about the state of X while it’s associated operation was executed
! The same is true of all atomic operations
22
![Page 37: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/37.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Mutexes and Sequential Consistency
23
class registry { serial_queue _q; using map_t = unordered_map<string, string>; shared_ptr<map_t> _map = make_shared<map_t>(); public: void set(string key, string value) { _q.async([_map = _map](string key, string value) { _map->emplace(move(key), move(value)); }, move(key), move(value)); } auto get(string key) -> future<string> { return _q.async([_map = _map](string key) { return _map->at(key); }, move(key)); } };
![Page 38: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/38.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Mutexes and Sequential Consistency
24
class registry { serial_queue _q; using map_t = unordered_map<string, string>; shared_ptr<map_t> _map = make_shared<map_t>(); public: void set(string key, string value) { _q.async([_map = _map](string key, string value) { _map->emplace(move(key), move(value)); }, move(key), move(value)); } auto get(string key) -> future<string> { return _q.async([_map = _map](string key) { return _map->at(key); }, move(key)); }
void set(vector<pair<string, string>> sequence) { _q.async([_map = _map](vector<pair<string, string>> sequence) { _map->insert(make_move_iterator(begin(sequence)), make_move_iterator(end(sequence))); }, move(sequence)); } };
![Page 39: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/39.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Mutexes and Sequential Consistency
! The transformation mutex to serial queue places an upper-bound ! Synchronization overhead ! Time to issue operation
25
![Page 40: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/40.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Threads and Tasks
! Thread: Execution environment consisting of a stack and processor state running in parallel to other threads ! Task: A unit of work, often a function, to be executed on a thread
! Tasks are scheduled on a thread pool to optimize machine utilization
26
![Page 41: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/41.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
C++14 and Tasks
! C++14 does not (really) have a task system !Threads ! Futures
! It is implementation defined if std::async() spins up a thread or executes on a thread pool.
27
![Page 42: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/42.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
! Portable Reference Implementation in C++14 ! Windows - Window Thread Pool and PPL ! Apple - Grand Central Dispatch (libdispatch) ! open source, runs on Linux and Android
! Intel TBB - many platforms ! open source
! HPX - many platforms ! open source
28
![Page 43: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/43.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
29
http://docs.oracle.com/cd/E19253-01/816-5137/ggedn/index.html
Core Core Core…
Thread Thread Thread
.
.
.
Task
Task
Task
Task
![Page 44: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/44.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
30
![Page 45: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/45.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
using lock_t = unique_lock<mutex>;
30
![Page 46: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/46.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
using lock_t = unique_lock<mutex>;
class notification_queue { deque<function<void()>> _q; mutex _mutex; condition_variable _ready;
30
![Page 47: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/47.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
using lock_t = unique_lock<mutex>;
class notification_queue { deque<function<void()>> _q; mutex _mutex; condition_variable _ready;
public: void pop(function<void()>& x) { lock_t lock{_mutex}; while (_q.empty()) _ready.wait(lock); x = move(_q.front()); _q.pop_front(); }
30
![Page 48: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/48.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
using lock_t = unique_lock<mutex>;
class notification_queue { deque<function<void()>> _q; mutex _mutex; condition_variable _ready;
public: void pop(function<void()>& x) { lock_t lock{_mutex}; while (_q.empty()) _ready.wait(lock); x = move(_q.front()); _q.pop_front(); }
template<typename F> void push(F&& f) { { lock_t lock{_mutex}; _q.emplace_back(forward<F>(f)); } _ready.notify_one(); }};
30
![Page 49: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/49.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
31
![Page 50: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/50.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
class task_system { const unsigned _count{thread::hardware_concurrency()}; vector<thread> _threads; notification_queue _q;
31
![Page 51: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/51.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
class task_system { const unsigned _count{thread::hardware_concurrency()}; vector<thread> _threads; notification_queue _q;
void run(unsigned i) { while (true) { function<void()> f; _q.pop(f); f(); } }
31
![Page 52: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/52.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
class task_system { const unsigned _count{thread::hardware_concurrency()}; vector<thread> _threads; notification_queue _q;
void run(unsigned i) { while (true) { function<void()> f; _q.pop(f); f(); } }
public: task_system() { for (unsigned n = 0; n != _count; ++n) { _threads.emplace_back([&, n]{ run(n); }); } }
31
![Page 53: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/53.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
class task_system { const unsigned _count{thread::hardware_concurrency()}; vector<thread> _threads; notification_queue _q;
void run(unsigned i) { while (true) { function<void()> f; _q.pop(f); f(); } }
public: task_system() { for (unsigned n = 0; n != _count; ++n) { _threads.emplace_back([&, n]{ run(n); }); } }
~task_system() { for (auto& e : _threads) e.join(); }
31
![Page 54: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/54.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
class task_system { const unsigned _count{thread::hardware_concurrency()}; vector<thread> _threads; notification_queue _q;
void run(unsigned i) { while (true) { function<void()> f; _q.pop(f); f(); } }
public: task_system() { for (unsigned n = 0; n != _count; ++n) { _threads.emplace_back([&, n]{ run(n); }); } }
~task_system() { for (auto& e : _threads) e.join(); }
template <typename F> void async_(F&& f) { _q.push(forward<F>(f)); }};
31
![Page 55: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/55.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
class notification_queue { deque<function<void()>> _q; bool _done{false}; mutex _mutex; condition_variable _ready;
public: void done() { { unique_lock<mutex> lock{_mutex}; _done = true; } _ready.notify_all(); }
bool pop(function<void()>& x) { lock_t lock{_mutex}; while (_q.empty() && !_done) _ready.wait(lock); if (_q.empty()) return false; x = move(_q.front()); _q.pop_front(); return true; }
template<typename F> void push(F&& f) { { lock_t lock{_mutex}; _q.emplace_back(forward<F>(f));
32
![Page 56: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/56.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
class notification_queue { deque<function<void()>> _q; bool _done{false}; mutex _mutex; condition_variable _ready;
public: void done() { { unique_lock<mutex> lock{_mutex}; _done = true; } _ready.notify_all(); }
bool pop(function<void()>& x) { lock_t lock{_mutex}; while (_q.empty() && !_done) _ready.wait(lock); if (_q.empty()) return false; x = move(_q.front()); _q.pop_front(); return true; }
template<typename F> void push(F&& f) { { lock_t lock{_mutex}; _q.emplace_back(forward<F>(f));
32
![Page 57: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/57.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
class notification_queue { deque<function<void()>> _q; bool _done{false}; mutex _mutex; condition_variable _ready;
public: void done() { { unique_lock<mutex> lock{_mutex}; _done = true; } _ready.notify_all(); }
bool pop(function<void()>& x) { lock_t lock{_mutex}; while (_q.empty() && !_done) _ready.wait(lock); if (_q.empty()) return false; x = move(_q.front()); _q.pop_front(); return true; }
template<typename F> void push(F&& f) { { lock_t lock{_mutex}; _q.emplace_back(forward<F>(f));
32
![Page 58: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/58.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
class notification_queue { deque<function<void()>> _q; bool _done{false}; mutex _mutex; condition_variable _ready;
public: void done() { { unique_lock<mutex> lock{_mutex}; _done = true; } _ready.notify_all(); }
bool pop(function<void()>& x) { lock_t lock{_mutex}; while (_q.empty() && !_done) _ready.wait(lock); if (_q.empty()) return false; x = move(_q.front()); _q.pop_front(); return true; }
template<typename F> void push(F&& f) { { lock_t lock{_mutex}; _q.emplace_back(forward<F>(f));
32
![Page 59: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/59.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
33
Core Core Core…
Thread Thread Thread
.
.
.
Task
Task
Task
Task
![Page 60: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/60.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
33
Core Core Core…
Thread Thread Thread
.
.
.
Task
Task
Task
Task
![Page 61: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/61.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Why No Raw Synchronization Primitives?
34
Object
thread
thread
thread
![Page 62: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/62.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Why No Raw Synchronization Primitives?
34
Object
thread
thread
thread
STOP
STOP
GO
![Page 63: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/63.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Why No Raw Synchronization Primitives?
34
Object
thread
thread
thread
STOP
STOP
GO
![Page 64: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/64.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
35
Core Core Core…
Thread Thread Thread
.
.
.
Task
Task
Task
Task
![Page 65: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/65.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
35
Core Core Core…
Thread Thread Thread
.
.
.
Task
Task
Task
Task
![Page 66: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/66.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
36
Core Core Core…
Thread Thread Thread
.
.
.
Task
Task
Task
.
.
.
Task
Task
Task
.
.
.
Task
Task
Task
Task
Scheduler
![Page 67: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/67.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
class task_system { const unsigned _count{thread::hardware_concurrency()}; vector<thread> _threads; vector<notification_queue> _q{_count}; atomic<unsigned> _index{0};
void run(unsigned i) { while (true) { function<void()> f; if (!_q[i].pop(f)) break; f(); } }
public: task_system() { }
~task_system() { for (auto& e : _q) e.done(); for (auto& e : _threads) e.join(); }
template <typename F> void async_(F&& f) { auto i = _index++; _q[i % _count].push(forward<F>(f)); } };
37
![Page 68: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/68.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
class task_system { const unsigned _count{thread::hardware_concurrency()}; vector<thread> _threads; vector<notification_queue> _q{_count}; atomic<unsigned> _index{0};
void run(unsigned i) { while (true) { function<void()> f; if (!_q[i].pop(f)) break; f(); } }
public: task_system() { }
~task_system() { for (auto& e : _q) e.done(); for (auto& e : _threads) e.join(); }
template <typename F> void async_(F&& f) { auto i = _index++; _q[i % _count].push(forward<F>(f)); } };
37
![Page 69: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/69.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
class task_system { const unsigned _count{thread::hardware_concurrency()}; vector<thread> _threads; vector<notification_queue> _q{_count}; atomic<unsigned> _index{0};
void run(unsigned i) { while (true) { function<void()> f; if (!_q[i].pop(f)) break; f(); } }
public: task_system() { }
~task_system() { for (auto& e : _q) e.done(); for (auto& e : _threads) e.join(); }
template <typename F> void async_(F&& f) { auto i = _index++; _q[i % _count].push(forward<F>(f)); } };
37
![Page 70: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/70.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
class task_system { const unsigned _count{thread::hardware_concurrency()}; vector<thread> _threads; vector<notification_queue> _q{_count}; atomic<unsigned> _index{0};
void run(unsigned i) { while (true) { function<void()> f; if (!_q[i].pop(f)) break; f(); } }
public: task_system() { }
~task_system() { for (auto& e : _q) e.done(); for (auto& e : _threads) e.join(); }
template <typename F> void async_(F&& f) { auto i = _index++; _q[i % _count].push(forward<F>(f)); } };
37
![Page 71: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/71.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
class task_system { const unsigned _count{thread::hardware_concurrency()}; vector<thread> _threads; vector<notification_queue> _q{_count}; atomic<unsigned> _index{0};
void run(unsigned i) { while (true) { function<void()> f; if (!_q[i].pop(f)) break; f(); } }
public: task_system() { }
~task_system() { for (auto& e : _q) e.done(); for (auto& e : _threads) e.join(); }
template <typename F> void async_(F&& f) { auto i = _index++; _q[i % _count].push(forward<F>(f)); } };
37
![Page 72: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/72.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
38
Core Core Core…
Thread Thread Thread
.
.
.
Task
Task
Task
.
.
.
Task
Task
Task
.
.
.
Task
Task
Task
Task
Scheduler
![Page 73: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/73.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
38
Core Core Core…
Thread Thread Thread
.
.
.
Task
Task
Task
.
.
.
Task
Task
Task
.
.
.
Task
Task
Task
Task
Scheduler
![Page 74: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/74.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
39
Core Core Core…
Thread Thread Thread
.
.
.
Task
Task
Task
.
.
.
Task
Task
Task
.
.
.
Task
Task
Task
Task
Scheduler
Task Stealing
![Page 75: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/75.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
class notification_queue { deque<function<void()>> _q; bool _done{false}; mutex _mutex; condition_variable _ready;
public: bool try_pop(function<void()>& x) { lock_t lock{_mutex, try_to_lock}; if (!lock || _q.empty()) return false; x = move(_q.front()); _q.pop_front(); return true; }
template<typename F> bool try_push(F&& f) { { lock_t lock{_mutex, try_to_lock}; if (!lock) return false; _q.emplace_back(forward<F>(f)); } _ready.notify_one(); return true; }
void done() { { unique_lock<mutex> lock{_mutex};
40
![Page 76: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/76.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
class notification_queue { deque<function<void()>> _q; bool _done{false}; mutex _mutex; condition_variable _ready;
public: bool try_pop(function<void()>& x) { lock_t lock{_mutex, try_to_lock}; if (!lock || _q.empty()) return false; x = move(_q.front()); _q.pop_front(); return true; }
template<typename F> bool try_push(F&& f) { { lock_t lock{_mutex, try_to_lock}; if (!lock) return false; _q.emplace_back(forward<F>(f)); } _ready.notify_one(); return true; }
void done() { { unique_lock<mutex> lock{_mutex};
40
![Page 77: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/77.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
class notification_queue { deque<function<void()>> _q; bool _done{false}; mutex _mutex; condition_variable _ready;
public: bool try_pop(function<void()>& x) { lock_t lock{_mutex, try_to_lock}; if (!lock || _q.empty()) return false; x = move(_q.front()); _q.pop_front(); return true; }
template<typename F> bool try_push(F&& f) { { lock_t lock{_mutex, try_to_lock}; if (!lock) return false; _q.emplace_back(forward<F>(f)); } _ready.notify_one(); return true; }
void done() { { unique_lock<mutex> lock{_mutex};
40
![Page 78: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/78.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
void run(unsigned i) { while (true) { function<void()> f;
for (unsigned n = 0; n != _count; ++n) { if (_q[(i + n) % _count].try_pop(f)) break; } if (!f && !_q[i].pop(f)) break;
f(); } }
public: task_system() { }
~task_system() { }
template <typename F> void async_(F&& f) { auto i = _index++;
for (unsigned n = 0; n != _count * K; ++n) { if (_q[(i + n) % _count].try_push(forward<F>(f))) return; }
_q[i % _count].push(forward<F>(f)); } };
41
![Page 79: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/79.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
void run(unsigned i) { while (true) { function<void()> f;
for (unsigned n = 0; n != _count; ++n) { if (_q[(i + n) % _count].try_pop(f)) break; } if (!f && !_q[i].pop(f)) break;
f(); } }
public: task_system() { }
~task_system() { }
template <typename F> void async_(F&& f) { auto i = _index++;
for (unsigned n = 0; n != _count * K; ++n) { if (_q[(i + n) % _count].try_push(forward<F>(f))) return; }
_q[i % _count].push(forward<F>(f)); } };
41
![Page 80: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/80.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
void run(unsigned i) { while (true) { function<void()> f;
for (unsigned n = 0; n != _count; ++n) { if (_q[(i + n) % _count].try_pop(f)) break; } if (!f && !_q[i].pop(f)) break;
f(); } }
public: task_system() { }
~task_system() { }
template <typename F> void async_(F&& f) { auto i = _index++;
for (unsigned n = 0; n != _count * K; ++n) { if (_q[(i + n) % _count].try_push(forward<F>(f))) return; }
_q[i % _count].push(forward<F>(f)); } };
41
![Page 81: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/81.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
42
Core Core Core…
Thread Thread Thread
.
.
.
Task
Task
Task
.
.
.
Task
Task
Task
.
.
.
Task
Task
Task
Task
Scheduler
Task Stealing
![Page 82: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/82.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building a Task System
42
Core Core Core…
Thread Thread Thread
.
.
.
Task
Task
Task
.
.
.
Task
Task
Task
.
.
.
Task
Task
Task
Task
Scheduler
Task Stealing
![Page 83: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/83.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Task System
! Compared to Apple’s Grand Central Dispatch (libdispatch)
43
![Page 84: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/84.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Task System
! Compared to Apple’s Grand Central Dispatch (libdispatch)
43
![Page 85: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/85.jpg)
© 2015 Adobe Systems Incorporated.
C++14 compatible async with libdispatch
template <class Function, class... Args> auto async(Function&& f, Args&&... args ) { using result_type = std::result_of_t<std::decay_t<Function>(std::decay_t<Args>...)>; using packaged_type = std::packaged_task<result_type()>; auto _p = new packaged_type(std::bind([_f = std::forward<Function>(f)](Args&... args) { return _f(std::move(args)...); }, std::forward<Args>(args)...)); auto result = _p->get_future();
dispatch_async_f(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), _p, [](void* p) { auto _p = static_cast<packaged_type*>(p); (*_p)(); delete _p; }); return result; }
44
![Page 86: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/86.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Task System
! Written with ASIO (Boost 1.62.0)
class task_system { io_service _service; vector<thread> _threads; unique_ptr<io_service::work> _work{make_unique<io_service::work>(_service)};
public: task_system() { for (unsigned n = 0; n != thread::hardware_concurrency(); ++n) { _threads.emplace_back([&]{ _service.run(); }); } }
~task_system() { _work.reset(); for (auto& e : _threads) e.join(); }
template <typename F> void async_(F&& f) { _service.post(forward<F>(f)); } };
45
![Page 87: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/87.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Task System
! Written with ASIO (Boost 1.62.0)
class task_system { io_service _service; vector<thread> _threads; unique_ptr<io_service::work> _work{make_unique<io_service::work>(_service)};
public: task_system() { for (unsigned n = 0; n != thread::hardware_concurrency(); ++n) { _threads.emplace_back([&]{ _service.run(); }); } }
~task_system() { _work.reset(); for (auto& e : _threads) e.join(); }
template <typename F> void async_(F&& f) { _service.post(forward<F>(f)); } };
45
![Page 88: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/88.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
No Raw Synchronization Primitives
46
Task
![Page 89: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/89.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
No Raw Synchronization Primitives
46
Task
![Page 90: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/90.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
No Raw Synchronization Primitives
46
Task
Object
![Page 91: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/91.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
No Raw Synchronization Primitives
46
Task
Object
Task
![Page 92: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/92.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
No Raw Synchronization Primitives
46
Task
Object
Task
![Page 93: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/93.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
No Raw Synchronization Primitives
46
Task
Object
Task
...
...
![Page 94: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/94.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
No Raw Synchronization Primitives
46
Task
Object
Task
...
...
?
![Page 95: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/95.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Futures
47
future<cpp_int> x = async([]{ return fibonacci<cpp_int>(1'000'000); });
// Do Something
cout << x.get() << endl;
![Page 96: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/96.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Futures
! Fibonacci is often used as an example for parallel algorithms ! Please stop…
47
future<cpp_int> x = async([]{ return fibonacci<cpp_int>(1'000'000); });
// Do Something
cout << x.get() << endl;
![Page 97: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/97.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Public Service Announcement - How to Write Fibonacci
template <typename T, typename N, typename O> T power(T x, N n, O op) { if (n == 0) return identity_element(op); while ((n & 1) == 0) { n >>= 1; x = op(x, x); } T result = x; n >>= 1; while (n != 0) { x = op(x, x); if ((n & 1) != 0) result = op(result, x); n >>= 1; } return result; }
48
![Page 98: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/98.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Public Service Announcement - How to Write Fibonacci
template <typename T, typename N, typename O> T power(T x, N n, O op) { if (n == 0) return identity_element(op); while ((n & 1) == 0) { n >>= 1; x = op(x, x); } T result = x; n >>= 1; while (n != 0) { x = op(x, x); if ((n & 1) != 0) result = op(result, x); n >>= 1; } return result; }
48
Egyptian Multiplication (Russian Peasant Algorithm) See “From Mathematics to Generic Programming” - Alex Stepanov and Dan Rose
![Page 99: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/99.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Public Service Announcement - How to Write Fibonacci
template <typename N> struct multiply_2x2 { array<N, 4> operator()(const array<N, 4>& x, const array<N, 4>& y) { return { x[0] * y[0] + x[1] * y[2], x[0] * y[1] + x[1] * y[3], x[2] * y[0] + x[3] * y[2], x[2] * y[1] + x[3] * y[3] }; } };
template <typename N> array<N, 4> identity_element(const multiply_2x2<N>&) { return { N(1), N(0), N(0), N(1) }; }
template <typename R, typename N> R fibonacci(N n) { if (n == 0) return R(0); return power(array<R, 4>{ 1, 1, 1, 0 }, N(n - 1), multiply_2x2<R>())[0]; }
49
![Page 100: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/100.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Public Service Announcement - How to Write Fibonacci
template <typename N> struct multiply_2x2 { array<N, 4> operator()(const array<N, 4>& x, const array<N, 4>& y) { return { x[0] * y[0] + x[1] * y[2], x[0] * y[1] + x[1] * y[3], x[2] * y[0] + x[3] * y[2], x[2] * y[1] + x[3] * y[3] }; } };
template <typename N> array<N, 4> identity_element(const multiply_2x2<N>&) { return { N(1), N(0), N(0), N(1) }; }
template <typename R, typename N> R fibonacci(N n) { if (n == 0) return R(0); return power(array<R, 4>{ 1, 1, 1, 0 }, N(n - 1), multiply_2x2<R>())[0]; }
49
![Page 101: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/101.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Futures
1953282128707757731632014947596256332443542996591873396953405194571625257887015694766641987634150146128879524335220236084625510912019560233744015438115196636156919962125642894303370113827800638002767411527927466669865578379318822832061271497583230334854893489572599230722912901928209264331627521730861460017912582042699659936020959339202005184862028402447343139811367418720203868480175318538621112878108240617741383293554561687606454065125954718029126547942894036981659206361019359291352135410376799082940320155702716115395031975973247782162957631629653356694777663285062345245593460647575025935813443457816767646258788590113727299073729478511448089572456191503507025589529116868550008802013233458747217794781447546792016090170642585629359747546532757575740077432034913428785189795354304734560307765078938767286539166799232817449361991523768149557632085371047859706188438731530582395627560879063107819004975169594709736713891745704555202135123350794403360712030504144685221041565037321067932275625864751191461141736034968121738023422478608029202109319249649040983239706683247054441763512526732455275419501683845206023007394959854279298297831204382115757645787692495583351402522152720662441809003259380753628491796680952971185071913798336788737704599136393339558142120369902616179721132509184002305532760710431647819097430043464779336328760146999612802392582947155731668894333945542929287187748774789204296166356536610796023919702109728472966709427334586344798048633944635211654971507261342768205479320931750798880101304160279825063541823440345587422367012826663569346112946131231283890600365473276602456931515185001832848315064548002997893598516123707404615822935444070174833951457586954749175026454212636426222472060048855462589961190475892101224280542898621594646662478564373572217775549876087685912030118551635668902010344639983977326638889036507841618070915452529927597352139574154777291460087943143391560604458251078235116627189263792331301464388059787946844487906057678629746098962742666356968247429338674020743655942605794479071193052258931590719386545525880429139747140181849169733838138446154843063123649290835584278078456131936457559117221369463381803116003078962116686528959537784364644023825163624497181973854441495631317140028503389282227413460301809422483721632185471727045281382407842563874736524914111808078386650633994537623920670051339187333107136069698189628284763245423299306272870457991293245741167533902274499963096566680922262516468582544557851349824144127261240158157538180984666671450069888391785518008943701890257219924852087429155602619177522812466062899678716652967848726848490504132849729771268801163997837643428020245225155010224035416988518537501584673881194047619720619603126534496759917893244478170702904446589571950228809157793897642423751814020998999581612314779022957811001686701867386198617971381398546662819695485537407073562286161655394280764184080921204793281668300598450478792940635631809747975515203509468276591874161090763750690276529436756153980326138890194485941004529227541880945735620795421899662963441346396955980991375010053760259440113617219176881147264
50
![Page 102: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/102.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Futures
4534355968226490642290139992796096559631067683585565483124248108070411241864496288278775013367814234676106615790155892833100345673846243104676900000936756893803676769777642059716492347060997973282994459039755683869105685411058885051979862321618071659608643166523833695792515458773247974295235724915183100135059940954313672345441853967639642257048786844333673556851153585056517249014177233301807239035068983866253233826620354847687722321662223383305226882245421258277211223435986491973881404168406609216954760818955479619408040043497601356464084611480778855378911228881396187039079060331474168814336581362769420066445056796904807027922065208551224508683937565519686130523209213804180827319885292805824696457556180161852004664494926234186485934292896521378574554544426221453176445385228867960454072522804961741905198550911362542849130027243353553345377968558497801959766365162905984572190434898213582212068569241211393131371321348657414408926700036655556324464997755685351468128988739170090705797083912419192306257054777274861099092451916822532682357814072123818963141147129610287340041050015549547086272721534936510345705849389706515725684266079756708385889612130516276472992631596744745949011999508491789521497159877319531917595916234240217185796967781020544965987668461439596506473322198532352137810818703064287550695189034358718163360412639767502090913354848015113595182411243263608049744737395896608759569909256138919905403404664655310556021101996525724843421071082933739200159651403373870955680756568226835379339839824880227237703197854614809323023472557966211738929885417307414847072116640441570575360458225614322429985978068323969654385552378378141386675079286837205802043347225419033684684301719893411568996526838242546875
50
0.72s to calculate 208,988 digits
![Page 103: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/103.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Futures
51
future<cpp_int> x = async([]{ return fibonacci<cpp_int>(1'000'000); }); // Do Something
cout << x.get() << endl;
![Page 104: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/104.jpg)
f(…)->r
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Futures
52
![Page 105: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/105.jpg)
f(…) r
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Futures
52
![Page 106: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/106.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Futures
! Futures allow minimal code transformations to express dependencies
53
![Page 107: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/107.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Exception Marshalling
future<cpp_int> x = async([]{ throw runtime_error("failure"); return fibonacci<cpp_int>(1'000'000); }); // Do Something
try { cout << x.get() << endl; } catch (const runtime_error& error) { cout << error.what() << endl; }
54
![Page 108: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/108.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Exception Marshalling
future<cpp_int> x = async([]{ throw runtime_error("failure"); return fibonacci<cpp_int>(1'000'000); }); // Do Something
try { cout << x.get() << endl; } catch (const runtime_error& error) { cout << error.what() << endl; }
54
failure
![Page 109: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/109.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
No Raw Synchronization Primitives
55
Task
Args
![Page 110: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/110.jpg)
future
© 2013 Adobe Systems Incorporated. All Rights Reserved.
No Raw Synchronization Primitives
55
Task
Args
Task
![Page 111: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/111.jpg)
future
© 2013 Adobe Systems Incorporated. All Rights Reserved.
No Raw Synchronization Primitives
55
Task
Args
Task...
...
![Page 112: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/112.jpg)
future
© 2013 Adobe Systems Incorporated. All Rights Reserved.
No Raw Synchronization Primitives
55
Task
Args
Task...
...future.get()
![Page 113: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/113.jpg)
future
© 2013 Adobe Systems Incorporated. All Rights Reserved.
No Raw Synchronization Primitives
55
Task
Args
Task...
...future.get()STOP
![Page 114: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/114.jpg)
future
© 2013 Adobe Systems Incorporated. All Rights Reserved.
No Raw Synchronization Primitives
55
Task
...
future.get()
Result
![Page 115: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/115.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Futures: What year is this?
! C++14 futures lack: ! Continuations - .then() ! Joins - when_all() ! Split ! Cancelation ! Progress Monitoring (Except Ready)
! And C++14 futures don’t compose (easily) to add these features
56
![Page 116: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/116.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Futures: Continuations
57
![Page 117: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/117.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Continuations
! Blocking on std::future.get() has two problems ! One thread resource is consumed, increasing contention ! Possibly causing a deadlock in our tasking system!
! Any subsequent non-dependent calculations on the task are also blocked
! C++14 doesn’t have continuations ! GCD has serialized queues and groups ! PPL has chained tasks ! TBB has flow graphs ! TS Concurrency will have .then() ! Boost futures have them now
58
![Page 118: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/118.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Futures: get() deadlock
59
.
.
.
Task
![Page 119: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/119.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Futures: get() deadlock
59
.
.
.
Task
Task
![Page 120: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/120.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Futures: get() deadlock
59
.
.
.
Task
Task
STOP
![Page 121: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/121.jpg)
.
.
.
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Futures: get() deadlock
60
.
.
.
Task
![Page 122: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/122.jpg)
.
.
.
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Futures: get() deadlock
60
.
.
.
Task
Task
.
.
.
Task
![Page 123: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/123.jpg)
.
.
.
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Futures: get() deadlock
60
.
.
.
Task
Task
STOP
.
.
.
Task
![Page 124: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/124.jpg)
.
.
.
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Futures: get() deadlock
60
.
.
.
Task
Task
STOP
.
.
.
Task
Task
![Page 125: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/125.jpg)
.
.
.
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Futures: get() deadlock
60
.
.
.
Task
Task
STOP
.
.
.
Task
Task
STOP
![Page 126: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/126.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Continuations
61
![Page 127: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/127.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Continuations
! Blocking on std::future.get() ! Very difficult to use safely with a thread pool ! C++14 allows std::async() to use a thread pool
61
![Page 128: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/128.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Continuations
! Blocking on std::future.get() ! Very difficult to use safely with a thread pool ! C++14 allows std::async() to use a thread pool
! Not just get() - any conditional blocking (condition variables, wait, …) is problematic with a task system
61
![Page 129: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/129.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Continuations
! Blocking on std::future.get() ! Very difficult to use safely with a thread pool ! C++14 allows std::async() to use a thread pool
! Not just get() - any conditional blocking (condition variables, wait, …) is problematic with a task system
Do call std::future.get() or std::future.wait() when the originating task, or any subordinate task, is on the same queue, even if it is a concurrent queue (i.e. a thread pool).
61
![Page 130: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/130.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Continuations
62
https://developer.apple.com/library/content/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html
![Page 131: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/131.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Continuations
future<cpp_int> x = async([]{ return fibonacci<cpp_int>(1'000); }); future<void> y = x.then([](future<cpp_int> x){ cout << x.get() << endl; }); // Do something y.wait();
63
![Page 132: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/132.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Continuations
future<cpp_int> x = async([]{ return fibonacci<cpp_int>(1'000); }); future<void> y = x.then([](future<cpp_int> x){ cout << x.get() << endl; }); // Do something y.wait();
63
43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875
![Page 133: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/133.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures vs Completion Handlers
! Completion handlers are callbacks, they must be known prior to the call ! No need to synchronize between invoking and setting the continuation
! Futures allow setting the continuation after the sending call is in flight ! Simpler to compose ! Require synchronization between invoking and setting the continuation
64
![Page 134: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/134.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Futures: Joins
65
![Page 135: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/135.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Task Systems
66
Task Task
Group2
*
![Page 136: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/136.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Task Systems
66
Task
Group1Result
*
![Page 137: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/137.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Task Systems
66
Task
Group0Result Result
*
![Page 138: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/138.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Continuations
auto x = async([]{ return fibonacci<cpp_int>(1'000'000); }); auto y = async([]{ return fibonacci<cpp_int>(2'000'000); }); auto z = when_all(std::move(x), std::move(y)).then([](auto f){ auto t = f.get(); return cpp_int(get<0>(t).get() * get<1>(t).get()); }); cout << z.get() << endl;
67
![Page 139: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/139.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Continuations
auto x = async([]{ return fibonacci<cpp_int>(1'000'000); }); auto y = async([]{ return fibonacci<cpp_int>(2'000'000); }); auto z = when_all(std::move(x), std::move(y)).then([](auto f){ auto t = f.get(); return cpp_int(get<0>(t).get() * get<1>(t).get()); }); cout << z.get() << endl;
67
f is a future tuple of futures
![Page 140: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/140.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Continuations
auto x = async([]{ return fibonacci<cpp_int>(1'000'000); }); auto y = async([]{ return fibonacci<cpp_int>(2'000'000); }); auto z = when_all(std::move(x), std::move(y)).then([](auto f){ auto t = f.get(); return cpp_int(get<0>(t).get() * get<1>(t).get()); }); cout << z.get() << endl;
67
f is a future tuple of futures
result is 626,964 digits
![Page 141: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/141.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Futures: Split
68
![Page 142: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/142.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Continuations
future<cpp_int> x = async([]{ return fibonacci<cpp_int>(100); }); future<cpp_int> y = x.then([](future<cpp_int> x){ return cpp_int(x.get() * 2); }); future<cpp_int> z = x.then([](future<cpp_int> x){ return cpp_int(x.get() / 15); });
69
![Page 143: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/143.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Continuations
future<cpp_int> x = async([]{ return fibonacci<cpp_int>(100); }); future<cpp_int> y = x.then([](future<cpp_int> x){ return cpp_int(x.get() * 2); }); future<cpp_int> z = x.then([](future<cpp_int> x){ return cpp_int(x.get() / 15); });
69
Assertion failed: (px != 0), function operator->, file shared_ptr.hpp, line 648.
![Page 144: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/144.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Continuations
! Desired behavior ! A future should behave as a regular type - a token for the actual value ! shared_futures let me “copy” them around and do multiple get() operations ! But not multiple continuations
70
![Page 145: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/145.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Continuations
! We can write a pseudo-copy, split().
71
split
![Page 146: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/146.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Continuations
72
![Page 147: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/147.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Continuations
future<cpp_int> x = async([]{ return fibonacci<cpp_int>(100); });
future<cpp_int> y = split(x).then([](future<cpp_int> x){ return cpp_int(x.get() * 2); }); future<cpp_int> z = x.then([](future<cpp_int> x){ return cpp_int(x.get() / 15); });
72
![Page 148: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/148.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Continuations
future<cpp_int> x = async([]{ return fibonacci<cpp_int>(100); });
future<cpp_int> y = split(x).then([](future<cpp_int> x){ return cpp_int(x.get() * 2); }); future<cpp_int> z = x.then([](future<cpp_int> x){ return cpp_int(x.get() / 15); });
future<void> done = when_all(std::move(y), std::move(z)).then([](auto f){ auto t = f.get(); cout << get<0>(t).get() << endl; cout << get<1>(t).get() << endl; });
done.wait();
72
![Page 149: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/149.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Continuations
future<cpp_int> x = async([]{ return fibonacci<cpp_int>(100); });
future<cpp_int> y = split(x).then([](future<cpp_int> x){ return cpp_int(x.get() * 2); }); future<cpp_int> z = x.then([](future<cpp_int> x){ return cpp_int(x.get() / 15); });
future<void> done = when_all(std::move(y), std::move(z)).then([](auto f){ auto t = f.get(); cout << get<0>(t).get() << endl; cout << get<1>(t).get() << endl; });
done.wait();
72
708449696358523830150 23614989878617461005
![Page 150: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/150.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Building Blocks
! Promise is the sending side of a future ! Promises are packaged with a function to formed a packaged task ! Packaged tasks handle the exception marshalling through a promise
73
![Page 151: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/151.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Promise
promise<int> x; future<int> y = x.get_future();
x.set_value(42); cout << y.get() << endl;
74
![Page 152: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/152.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Promise
promise<int> x; future<int> y = x.get_future();
x.set_value(42); cout << y.get() << endl;
74
42
![Page 153: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/153.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Split
template <typename T> auto split(future<T>& x) {
auto tmp = std::move(x);
promise<T> p; x = p.get_future(); // replace x with new future
return tmp.then([_p = move(p)](auto _tmp) mutable { auto value = _tmp.get(); _p.set_value(value); // assign to new "x" future return value; // return value through future result }); }
75
![Page 154: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/154.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Split
template <typename T> auto split(future<T>& x) {
auto tmp = std::move(x);
promise<T> p; x = p.get_future(); // replace x with new future
return tmp.then([_p = move(p)](auto _tmp) mutable { auto value = _tmp.get(); _p.set_value(value); // assign to new "x" future return value; // return value through future result }); }
75
x
![Page 155: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/155.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Split
template <typename T> auto split(future<T>& x) {
auto tmp = std::move(x);
promise<T> p; x = p.get_future(); // replace x with new future
return tmp.then([_p = move(p)](auto _tmp) mutable { auto value = _tmp.get(); _p.set_value(value); // assign to new "x" future return value; // return value through future result }); }
75
tmp
x
![Page 156: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/156.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Split
template <typename T> auto split(future<T>& x) {
auto tmp = std::move(x);
promise<T> p; x = p.get_future(); // replace x with new future
return tmp.then([_p = move(p)](auto _tmp) mutable { auto value = _tmp.get(); _p.set_value(value); // assign to new "x" future return value; // return value through future result }); }
75
x
tmp
p
![Page 157: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/157.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Split
template <typename T> auto split(future<T>& x) {
auto tmp = std::move(x);
promise<T> p; x = p.get_future(); // replace x with new future
return tmp.then([_p = move(p)](auto _tmp) mutable { auto value = _tmp.get(); _p.set_value(value); // assign to new "x" future return value; // return value through future result }); }
75
x
p
then result_tmp
tmp
![Page 158: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/158.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Split
template <typename T> auto split(future<T>& x) {
auto tmp = std::move(x);
promise<T> p; x = p.get_future(); // replace x with new future
return tmp.then([_p = move(p)](auto _tmp) mutable { auto value = _tmp.get(); _p.set_value(value); // assign to new "x" future return value; // return value through future result }); }
75
x
then result_p
_tmp
tmp
p
![Page 159: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/159.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Split
template <typename T> auto split(future<T>& x) {
auto tmp = std::move(x);
promise<T> p; x = p.get_future(); // replace x with new future
return tmp.then([_p = move(p)](auto _tmp) mutable { auto value = _tmp.get(); _p.set_value(value); // assign to new "x" future return value; // return value through future result }); }
75
x
then result_p
_tmp
![Page 160: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/160.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Split
template <typename T> auto split(future<T>& x) { auto tmp = std::move(x); promise<T> p; x = p.get_future(); // replace x with new future return tmp.then([_p = std::move(p)](auto _tmp) mutable { if (_tmp.has_exception()) { auto error = _tmp.get_exception_ptr(); _p.set_exception(error); rethrow_exception(error); }
auto value = _tmp.get(); _p.set_value(value); // assign to new "x" future return value; // return value through future result }); }
76
![Page 161: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/161.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Futures: Continuations
future<cpp_int> x = async([]{ return fibonacci<cpp_int>(100); }); future<cpp_int> y = split(x).then([](future<cpp_int> x){ return cpp_int(x.get() * 2); }); future<cpp_int> z = x.then([](future<cpp_int> x){ return cpp_int(x.get() / 15); }); future<void> done = when_all(std::move(y), std::move(z)).then([](auto f){ auto t = f.get(); cout << get<0>(t).get() << endl; cout << get<1>(t).get() << endl; });
done.wait();
77
708449696358523830150 23614989878617461005
![Page 162: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/162.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Cancelation
78
![Page 163: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/163.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Cancelation
! When the (last) future destructs !The associated task that has not started, should not execute (NOP) !The resource held by that task should be released ! Since that task may hold futures for other tasks, the system unravels
78
![Page 164: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/164.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Cancelation
! When the (last) future destructs !The associated task that has not started, should not execute (NOP) !The resource held by that task should be released ! Since that task may hold futures for other tasks, the system unravels
! I do not know of a good way to compose such cancelation with current futures ! Except to create something more complex than re-implementing futures
78
![Page 165: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/165.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Cancelation
79
![Page 166: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/166.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Cancelation
79
![Page 167: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/167.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Cancelation
79
![Page 168: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/168.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Cancelation
79
![Page 169: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/169.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
stlab future library
80
![Page 170: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/170.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
stlab future library
! Currently supports ! Multiple continuations and copy ! Optimized for rvalues
! Join (When All, When Any) ! Cancelation on Destruction (and explicit reset) ! And detach
! https://github.com/stlab/libraries/tree/develop! Thanks to Felix Petriconi
80
![Page 171: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/171.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.
Channels
81
![Page 172: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/172.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
What if we persist the graph?
82
![Page 173: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/173.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
What if we persist the graph?
! Allow multiple invocations of the tasks by setting the source values ! Each change triggers a notification to the sink values ! This is a reactive programming model and futures are known as behaviors or channels
83
![Page 174: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/174.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Accumulators and Generator
! Each operation does not have to be a 1:1 mapping of input to output ! Coroutines are one way to write n:m functions
84
![Page 175: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/175.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Channels
channel<int> send;
auto hold = send | [](const receiver<int>& r) { int sum = 0; while(auto v = co_await r) { sum += v.get(); } return sum; } | [](int x){ cout << x << '\n'; };
send(1); send(2); send(3); send.close();
85
![Page 176: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/176.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Channels
channel<int> send;
auto hold = send | [](const receiver<int>& r) { int sum = 0; while(auto v = co_await r) { sum += v.get(); } return sum; } | [](int x){ cout << x << '\n'; };
send(1); send(2); send(3); send.close();
85
6
![Page 177: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/177.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Channels
struct sum { process_state_scheduled _state = await_forever; int _sum = 0;
void await(int n) { _sum += n; }
int yield() { _state = await_forever; return _sum; }
void close() { _state = yield_immediate; }
const auto& state() const { return _state; } };
86
![Page 178: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/178.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Flow Control
87
![Page 179: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/179.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Flow Control
87
![Page 180: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/180.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Flow Control
88
![Page 181: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/181.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Flow Control
88
![Page 182: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/182.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Flow Control
88
![Page 183: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/183.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Flow Control
88
![Page 184: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/184.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Flow Control
88
![Page 185: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/185.jpg)
![Page 186: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/186.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Channels
struct render { process_state_scheduled _state = await_forever; bool _final = false; parameters _params;
void await(parameters params) { _final = false; _state = await_immediate; _params = params; }
frame yield() { auto result = render_frame(_params, _final); _final = !_final; _state = _final ? await_immediate : await_forever; return result; }
void close() { if (_state == await_immediate) _state = yield_immediate; }
const auto& state() const { return _state; } };
90
![Page 187: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/187.jpg)
© 2013 Adobe Systems Incorporated. All Rights Reserved.
Final Thoughts
! Perhaps representing such systems as if it where imperative code is not the correct approach ! Instead a graph description can be compiled and statically validated
! Slides and code from talk: ! http://sean-parent.stlab.cc/papers-and-presentations
! Experimental future and channel library: ! https://github.com/stlab/libraries/tree/develop ! Thanks to Felix Petriconi
! Communicating Sequential Processes (C. A. R. Hoare) ! http://usingcsp.com/cspbook.pdf
91
![Page 188: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/188.jpg)
© 2015 Adobe Systems Incorporated. 92
![Page 189: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/189.jpg)
© 2015 Adobe Systems Incorporated. 92
![Page 190: Better Code: Concurrency · © 2014 Adobe Systems Incorporated. All Rights Reserved. Be!er Code! Regular Type ! Goal: Implement Complete and Efficient Types ! Algorithms ! Goal: No](https://reader033.fdocuments.us/reader033/viewer/2022041709/5e4719b541a75370ae6dc428/html5/thumbnails/190.jpg)
© 2014 Adobe Systems Incorporated. All Rights Reserved.