Non-obvious lifetime issue with std::promise and std::future

后端 未结 4 1757
[愿得一人]
[愿得一人] 2021-02-05 18:10

This question is very similar to a previous one here: race-condition in pthread_once()?

It is essentially the same issue - the lifetime of a std::promise en

4条回答
  •  被撕碎了的回忆
    2021-02-05 18:24

    The canonical answer is to never std::bind to this but rather to a std::weak_ptr. When you get the callback, lock() it and check for NULL before invoking the callback.

    Or, re-stated, never call a member function (from outside) from a scope that doesn't hold a shared_ptr to the object.

提交回复
热议问题