From 8d510416da38bfc47d8a6fca5af333069a207cfa Mon Sep 17 00:00:00 2001 From: Jason Leng Date: Wed, 21 Dec 2016 23:32:08 -0800 Subject: [PATCH] Update future interrupts example in README Summary: Updated the future interrupts example to get rid of the shared pointer circular reference problem Reviewed By: djwatson Differential Revision: D4360037 fbshipit-source-id: cb959929a508df4dcf3b81d01012bc55044a0b17 --- folly/futures/README.md | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/folly/futures/README.md b/folly/futures/README.md index 381070e5..ec50fa8d 100644 --- a/folly/futures/README.md +++ b/folly/futures/README.md @@ -824,13 +824,19 @@ Although inspired by the C++11 std::future interface, it is not a drop-in replac

Interrupts allow Future holders to send signals in the form of exceptions to Promise holders, who are free to handle the interrupt as they please (or not at all). For example:

auto p = std::make_shared<Promise<int>>();
-p->setInterruptHandler([p](const exception_wrapper& e){
+p->setInterruptHandler([weakPromise = folly::to_weak_ptr(p)](
+    const exception_wrapper& e) {
+  auto promise = weakPromise.lock();
   // Handle the interrupt. For instance, we could just fulfill the Promise
   // with the given exception:
-  p->setException(e);
-  
-  // Or maybe we want the Future to complete with some special value
-  p->setValue(42);
+  if (promise) {
+    promise->setException(e);
+  }
+
+  // Or maybe we want the Future to complete with some special value
+  if (promise) {
+    promise->setValue(42);
+  }
 
   // Or maybe we don't want to do anything at all! Including not setting
   // this handler in the first place.
-- 
2.34.1