2017-03-01 35 views
5

它是否安全,如std::mutexstd::promise<T>mutable,还是取决于T?如:是std :: promise <T>线程安全吗?

using Data = std::tuple<bool, int, int>; 

struct X { 

    std::future<Data> prepare() const { 
     return m_promise.get_future(); 
    } 

    void asyncHandler(int a, int b) const { 
     m_promise.set_value({true, a, b}); 
    } 

    void cancel() const { 
     m_promise.set_value({false, 0, 0}); 
    } 

    mutable std::promise<Data> m_promise; // Is this safe? 
}; 


void performAsyncOp(const X& x) { 
    std::future<Data> fut = x.prepare(); 
    dispatch(x); 
    std::future_status result = fut.wait_for(std::chrono::milliseconds(150)); 
    if (result == std::future_status::timeout) { 
     x.cancel(); 
    } 

    handleResult(fut.get()); 
} 
+0

事实证明,由于期货不能重置,这种模式无论如何都被打破了。我以为future.get()会重置它,但它不会。我很努力地看到所有这些承诺/未来的东西。 – James

回答

4

让我们在API的详细研究:

// retrieving the result 
future<R> get_future(); 

// setting the result 
void set_value(see below); 
void set_exception(exception_ptr p); 

// setting the result with deferred notification 
void set_value_at_thread_exit(see below); 
void set_exception_at_thread_exit(exception_ptr p); 

的方法都没有被标记const,所以我们不能推断出从眼前这个常量性的任何知识。然而,该标准强制以下方法(c.f. 33.6.6.2):set_­valueset_­exception,set_­value_­at_­thread_­exitset_­exception_­at_­thread_­exit的线程安全性。

这留下get_future关于线程安全的未指定。但是,如果调用多次,则get_future会引发异常,c.f. 33.6.6.14.1。所以从多个线程调用get_future从实际角度来看并不合理。

当我看到时,无法同时拨打get_futureset方法和get_future(无论它是否会抛出),无法保证线程安全。