|
| 1 | +## Module name: Asynchronous programming |
| 2 | + |
| 3 | +_Skeleton descriptions are typeset in italic text,_ |
| 4 | +_so please don't remove these descriptions when editing the topic._ |
| 5 | + |
| 6 | +### Overview |
| 7 | + |
| 8 | +_Provides a short natural language abstract of the module’s contents._ |
| 9 | +_Specifies the different levels of teaching._ |
| 10 | + |
| 11 | +------------------------------------------------------------------------ |
| 12 | +Level Objective |
| 13 | +----------------- ------------------------------------------------------ |
| 14 | +Foundational --- Knowledge about build systems |
| 15 | + |
| 16 | +Main --- Usage of build system to compile a executable |
| 17 | + |
| 18 | +Advanced --- Add external libraries as a dependencies |
| 19 | + |
| 20 | +------------------------------------------------------------------------ |
| 21 | + |
| 22 | +### Motivation |
| 23 | + |
| 24 | +_Why is this important?_ |
| 25 | +_Why do we want to learn/teach this topic?_ |
| 26 | + |
| 27 | +* Asynchronous programming allows for non-blocking functions launches and enables that functions are executed asynchronoulsy. |
| 28 | +* Asynchronous programming is one form of parallism and concurrency included in the C++ standard |
| 29 | + |
| 30 | +### Topic introduction |
| 31 | + |
| 32 | +_Very brief introduction to the topic._ |
| 33 | + |
| 34 | +Build systems are used to configure, build, and install complex C++ projects. |
| 35 | + |
| 36 | + |
| 37 | +### Foundational: Knowledge about build systems |
| 38 | + |
| 39 | +#### Background/Required Knowledge |
| 40 | + |
| 41 | +A student: |
| 42 | +* Should know [lambdas](../functions/lambdas.md) |
| 43 | + |
| 44 | +#### Student outcomes |
| 45 | + |
| 46 | +_A list of things "a student should be able to" after the curriculum._ |
| 47 | +_The next word should be an action word and testable in an exam._ |
| 48 | +_Max 5 items._ |
| 49 | + |
| 50 | +A student should be able to: |
| 51 | + |
| 52 | +1. To explain what asynchronous programming is |
| 53 | +2. To explain what a futures and promise are |
| 54 | + |
| 55 | +#### Caveats |
| 56 | + |
| 57 | +_This section mentions subtle points to understand, like anything resulting in |
| 58 | +implementation-defined, unspecified, or undefined behavior._ |
| 59 | + |
| 60 | +None |
| 61 | + |
| 62 | +#### Points to cover |
| 63 | + |
| 64 | +_This section lists important details for each point._ |
| 65 | + |
| 66 | +* Mention how to launch a function or lambda asynchronously using `std::async` |
| 67 | +* Mention how to use a `std::future` as a place holder for the result |
| 68 | + |
| 69 | +### Main: Launch functions asynchronous and obtain the results |
| 70 | + |
| 71 | +#### Background/Required Knowledge |
| 72 | + |
| 73 | +* All of the above. |
| 74 | + |
| 75 | +#### Student outcomes |
| 76 | + |
| 77 | +A student should be able to: |
| 78 | + |
| 79 | +1. Do define a function or lambda for the computational task |
| 80 | +2. Launch the function or lambda asynchronous and obtain the results |
| 81 | + |
| 82 | +#### Caveats |
| 83 | + |
| 84 | +The concept of asyncrhonous programming is no easy digestiable for most students. |
| 85 | + |
| 86 | + |
| 87 | +#### Points to cover |
| 88 | + |
| 89 | +* The header `<future>` needs to be includes |
| 90 | +* The return type of the function or lambda will the the template type of the future |
| 91 | +* The first argument of `std::async` is the function or lambda and after that all arguments are providesd |
| 92 | + |
| 93 | +Example using a function |
| 94 | +``` |
| 95 | +void print_square(double a) |
| 96 | +{ |
| 97 | + std::cout << "Result=" << a * a << std::endl; |
| 98 | +} |
| 99 | +
|
| 100 | +std::future<void> f = std::async(print,5.0); |
| 101 | +// We could do other work here |
| 102 | +f.get() |
| 103 | +``` |
| 104 | + |
| 105 | +Example using lambdas |
| 106 | +``` |
| 107 | +// Compute the sum |
| 108 | +std::future<double> f1 = std::async([](double a, double b){ return a + b;}); |
| 109 | +// Compute the square |
| 110 | +std::future<double> f2 = std::async([](double a){ return a * a;}); |
| 111 | +
|
| 112 | +// Gather the results and add them up |
| 113 | +double res = f1.get() + f2.get(); |
| 114 | +``` |
| 115 | + |
| 116 | +### Advanced |
| 117 | + |
| 118 | +_These are important topics that are not expected to be covered but provide |
| 119 | +guidance where one can continue to investigate this topic in more depth._ |
| 120 | + |
| 121 | +* How to build libraries |
| 122 | +* How to have external libraries be downloaded during the build process |
| 123 | +* Mention that build systems provide support for unit testing |
| 124 | + |
0 commit comments