1

Suppose I have a thread that is running a packaged_task. I am not interested in the return value of the task and only wish for the thread to run the task silently in the background. As such, I never use the get_future() to obtain any handle on the promise of the task.

My concern is, when the packaged_task is completed and the destructor ~packaged_task is called, it states that

As with std::promise::~promise, if the shared state is abandoned before it was made ready, an std::future_error exception is stored with the error code std::future_errc::broken_promise).

My question is, assuming I never get a handle on the promise via get_future(), can I safely ignore the std::future_errc::broken_promise exception?

My question is motivated by trying to modify this thread pool library to ignore return values since I'm only using it with void-returning functions. I was thinking to simply change the return type of the enqueue function to return void and remove the get_future() on line 72.

Rufus
  • 5,111
  • 4
  • 28
  • 45
  • Related: https://stackoverflow.com/questions/21531096/can-i-use-stdasync-without-waiting-for-the-future-limitation – Rufus Apr 12 '21 at 01:59

1 Answers1

1

A packaged_task can only ever have a broken promise if you never actually call it. That is, if you don't invoke operator() at all (or the similar make_ready_at_thread_exit) before the packaged_task is destroyed. Calling the task will make the state ready (either with an exception or a "value", which can be void).

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • So once the `packaged_task` is called, I can safely destroy it regardless of whether or not the `promise` was read or not? – Rufus Apr 13 '21 at 07:49