2012-11-26 58 views
3

在c#中我非常喜歡AutoResetEventManualResetEventWaitHandle.WaitAll。 我應該在C++中使用什麼(可以使用boost)具有相同的功能?我需要代碼才能移植,因此我可以在Windows和Linux上運行它。什麼是C#的AutoResetEvent/ManualResetEvent的便攜式模擬?

+2

可能你願意描述這些事情做什麼? – Omnifarious

+0

我不知道(我不知道C#),但你可以使用標準的線程機制,如信號量。 –

+1

@Omnifarious很可能這篇文章可以幫助http://www.codeproject.com/Articles/39040/Auto-and-Manual-Reset-Events-Revisited這很難用兩個詞來解釋... – javapowered

回答

0

我還沒有完整閱讀鏈接,我想你可以使用期貨/承諾來實現它們。

該承諾用於設置事件值,未來等待值。

爲了獲得自動/手動重置,您將需要使用智能指針進行額外的間接尋址,以便重新分配承諾/未來。

明年如下想法:

template <typename T> 
class AutoResetEvent 
{ 
    struct Impl { 
    promise<T> p; 
    future<T> f; 
    Impl(): p(), f(p) {} 
    }; 
    scoped_ptr<Impl> ptr; 
public: 
    AutoResetEvent() : ptr(new Impl()) {} 
    set_value(T const v) { 
    ptr->p.set_value(v); 
    } 
    T get() { 
    T res = ptr->f.get(); 
    ptr.reset(new Impl()); 
    } 
    void wait() { 
    ptr->f.wait(); 
    ptr.reset(new Impl()); 
    } 
}; 

你需要多一點點,使他們移動。

等待幾個期貨只是在你想等待的事件的容器上迭代。