std::packaged_task enables you to write a simple wrapper for a callable, which you can invoke later.
To deal with std::packaged_task, you have to perform four steps usually:
- Wrap the task
- Create the future
- Perform the calculation
- Pick up the result
These steps are easy to get with an example.
The job of the program is not so heavy. Calculate the sum from 0 to 10000 with the help of four threads and sum up the results with the associated futures. Of course, you can use the Gaußschen Summenformel. (Strange, there is no English page describing this famous algorithm. But math is international.).
I pack the work packages in std::packaged_task (lines 28 – 31) objects in the first step. Work packages are instances of the class SumUp (lines 9 – 16). The call operator does the current work (lines 11-15). The call operator sums up all numbers from beg to end and returns the sum. std::packaged_task in lines 28 – 31 can deal with callables that need two ints and return an int.
Now, I have to create in the second step the future objects with the help of std::packaged_task objects. Precisely that is done in lines 34 to 37. The packaged_task is the promise in the communication channel. In these lines, I define the type of the future: std::future<int> sumResult1= sumTask1.get_future(), but of course, I can do it also implicitly: auto sumResult1= sumTask1.get_future().
In the third step, the work takes place. The packaged_task is moved onto the std::deque (lines 40 – 44). Each packaged_task (lines 51 – 58) is executed in the while loop. To do that, I move the head of the std::deque in a std::packaged_task (line 52), move the packaged_task in a new thread (line 54) and let it run in the background (line 56). std::packaged_task object is not copyable. That’s why I used move semantic in lines 52 and 54. This restriction holds for all promises but also futures and threads. There is one exception to this rule: std:.shared_future.
In the fourth and last step, the four futures ask with the get call for the results and sum them up (line 61).
Honestly, std::packaged_task was not made for a simple use case like std::async. But the result is simple.
C++11 has a function std::thread_hardware_concurrency. It provides a hint about the number of cores on your system. If the C++ runtime has no glue, it conforms to the standard to return 0. So you should verify that value in your program (line 31). With the current GCC, Clang, or Microsoft compiler, I get the right answer 4. I use this number of cores In the variation of the program packagedTask.cpp, to adjust the software to my hardware. So, my hardware is fully utilized.
The next post will be about futures and promises. (Proofreader Alexey Elymanov)
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, moon, and Philipp Lenk.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
|My special thanks to SHAVEDYAKS|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,