MultiThreading
|
[Accessing results] [The shared state] [Most used features]
A thread function can store its results and also exceptions within a std::promise:
Remark: You can only store a value OR an exception.
Method | Description |
---|---|
f.valid() | returns true if the future has a valid state. Only in this case the following functions are available |
f.get() | returns the result/exception provided by the thread function; performs a blocking wait if the thread function has not yet finished; a deferred thread execution may be started here. The call will invalidate the state, i.e. f.get() can only be called once! |
f.wait() | performs a blocking wait if the thread function has not yet finished; a deferred thread execution may be started here. |
f.wait_for(chrono::seconds(1.5)) | performs a blocking wait until the thread function has finished or the given timeout has elapsed; a deferred thread execution is NOT started. |
For more info see std::future - complete reference at CppReference.com
Method | Description |
---|---|
p.get_future() | returns a future object to be used for accessing the result stored within the promise |
p.set_value(v) | sets the given value as return value within the shared state and makes the state ready |
p.set_value_at_thread_exit(v) | sets the given value as return value within the shared state and makes the state ready at the end of the thread |
p.set_exception(e) | sets the given exception within the shared state and makes the state ready |
p.set_exception_at_thread_exit(e) | sets the given exception within the shared state and makes the state ready at the end of the thread |
For more info see std::promise - complete reference at CppReference.com