C++ Library Extensions 2022.12.09
To help learn modern C++ programming
040-future_promise.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2
5
6void division(std::promise<int>& promise, int a, int b)
7{
8 try
9 {
10 if(b==0)
11 {
12 Tpf_ThrowDebugException("Division By Zero");
13 }
14 else
15 {
16 promise.set_value(a/b);
17 return;
18 }
19
20 }catch(...)
21 {
22 promise.set_exception(std::current_exception());
23 return;
24 }
25}
26
28{
29 std::promise<int> promise;
30 auto future = promise.get_future();
31
32 // running in the background
33 std::thread t{division, std::ref(promise), 8, 0};
34 t.detach();
35
36
37 try
38 {
39 stream << "8 / 2 = " << future.get() << endl;
40 }
41 catch(const std::exception& e)
42 {
43 stream << e << endl;
44 }
45}
46
47int main()
48{
50 stream << "Okay ..." << endl;
51}
reference_wrapper< Type > ref(Type &val) noexcept
tpf::sstream stream
void division(std::promise< int > &promise, int a, int b)
auto endl
void example_for_promise()
int main()
constexpr auto endl
Definition: tpf_output.hpp:973
Stream output operators << are implemented.
#define Tpf_ThrowDebugException(debug_message)
Throw a debug_exception with message as argument.
Definition: tpf_types.hpp:1416