|
4 | 4 | \frametitlecpp[11]{Basic concurrency} |
5 | 5 | \begin{block}{Threading} |
6 | 6 | \begin{itemize} |
7 | | - \item new object std::thread in \textless{}thread\textgreater{} header |
| 7 | + \item new object \mintinline{cpp}{std::thread} in \mintinline{cpp}{<thread>} header |
8 | 8 | \item takes a function as argument of its constructor |
9 | 9 | \item must be detached or joined before the main thread terminates |
10 | | - \item \cpp20: std::jthread automatically joins at destruction |
| 10 | + \item \cpp20: \mintinline{cpp}{std::jthread} automatically joins at destruction |
11 | 11 | \end{itemize} |
12 | 12 | \end{block} |
13 | 13 | \pause |
14 | 14 | \begin{exampleblock}{Example code} |
15 | 15 | \begin{cppcode*}{} |
16 | | - void doSth() {...} |
17 | | - void doSthElse() {...} |
| 16 | + void foo() {...} |
| 17 | + void bar() {...} |
18 | 18 | int main() { |
19 | | - std::thread t1(doSth); |
20 | | - std::thread t2(doSthElse); |
| 19 | + std::thread t1(foo); |
| 20 | + std::thread t2(bar); |
21 | 21 | for (auto t: {&t1,&t2}) t->join(); |
22 | 22 | } |
23 | 23 | \end{cppcode*} |
|
53 | 53 | \begin{block}{Concept} |
54 | 54 | \begin{itemize} |
55 | 55 | \item separation of the specification of what should be done and the retrieval of the results |
56 | | - \item ``start working on this, and ping me when it's ready'' |
| 56 | + \item ``start working on this, and let me check if it's done'' |
57 | 57 | \end{itemize} |
58 | 58 | \end{block} |
59 | 59 | \pause |
60 | 60 | \begin{block}{Practically} |
61 | 61 | \begin{itemize} |
62 | | - \item std::async function launches an asynchronous task |
63 | | - \item std::future template allows to handle the result |
| 62 | + \item \mintinline{cpp}{std::async} function launches an asynchronous task |
| 63 | + \item \mintinline{cpp}{std::future<T>} allows to retrieve the result |
64 | 64 | \end{itemize} |
65 | 65 | \end{block} |
66 | 66 | \pause |
67 | 67 | \begin{exampleblock}{Example code} |
68 | 68 | \begin{cppcode*}{} |
69 | | - int computeSth() {...} |
70 | | - std::future<int> res = std::async(computeSth); |
71 | | - std::cout << res->get() << std::endl; |
| 69 | + int f() {...} |
| 70 | + std::future<int> res = std::async(f); |
| 71 | + std::cout << res->get(); |
72 | 72 | \end{cppcode*} |
73 | 73 | \end{exampleblock} |
74 | 74 | \end{frame} |
75 | 75 |
|
76 | 76 | \begin{frame}[fragile] |
77 | 77 | \frametitlecpp[11]{Mixing the two} |
78 | | - \begin{block}{Is async running concurrent code ?} |
| 78 | + \begin{block}{Is async running concurrently ?} |
79 | 79 | \begin{itemize} |
80 | 80 | \item it depends! |
81 | 81 | \item you can control this with a launch policy argument |
82 | 82 | \begin{description} |
83 | | - \item[std::launch::async] spawns a thread for immediate execution |
| 83 | + \item[std::launch::async] start executing immediately on a separate thread (may be a new thread, a thread pool, ...) |
84 | 84 | \item[std::launch::deferred] causes lazy execution in current thread |
85 | 85 | \end{description} |
86 | 86 | \begin{itemize} |
87 | | - \item execution starts when get() is called |
| 87 | + \item execution starts when \mintinline{cpp}{get()} is called on the returned future |
88 | 88 | \end{itemize} |
89 | 89 | \item default is not specified! |
90 | 90 | \end{itemize} |
91 | 91 | \end{block} |
92 | 92 | \pause |
93 | 93 | \begin{exampleblock}{Usage} |
94 | 94 | \begin{cppcode*}{} |
95 | | - int computeSth() {...} |
96 | | - auto res = std::async(std::launch::async, |
97 | | - computeSth); |
98 | | - auto res2 = std::async(std::launch::deferred, |
99 | | - computeSth); |
| 95 | + int f() {...} |
| 96 | + auto res1 = std::async(std::launch::async, f); |
| 97 | + auto res2 = std::async(std::launch::deferred, f); |
100 | 98 | \end{cppcode*} |
101 | 99 | \end{exampleblock} |
102 | 100 | \end{frame} |
103 | 101 |
|
104 | 102 | \begin{frame}[fragile] |
105 | 103 | \frametitlecpp[11]{Fine grained control on asynchronous execution} |
106 | | - \begin{block}{std::packaged\_task template} |
| 104 | + \begin{block}{\texttt{std::packaged\_task} template} |
107 | 105 | \begin{itemize} |
108 | 106 | \item creates an asynchronous version of any function-like object |
109 | 107 | \begin{itemize} |
110 | 108 | \item identical arguments |
111 | | - \item returns a std::future |
| 109 | + \item returns a \mintinline{cpp}{std::future} |
112 | 110 | \end{itemize} |
113 | 111 | \item provides access to the returned future |
114 | 112 | \item associated with threads, gives full control on execution |
|
117 | 115 | \pause |
118 | 116 | \begin{exampleblock}{Usage} |
119 | 117 | \begin{cppcode*}{} |
120 | | - int task() { return 42; } |
121 | | - std::packaged_task<int()> pckd_task(task); |
122 | | - auto future = pckd_task.get_future(); |
123 | | - pckd_task(); |
| 118 | + int f() { return 42; } |
| 119 | + std::packaged_task<int()> task(f); |
| 120 | + auto future = task.get_future(); |
| 121 | + task(); |
124 | 122 | std::cout << future.get() << std::endl; |
125 | 123 | \end{cppcode*} |
126 | 124 | \end{exampleblock} |
|
0 commit comments