一部の並行プログラミングでは、Java のCountDownLatchの概念を使用できます。C++11 に相当するものはありますか、またはその概念は C++ で何と呼ばれますか?
私が望むのは、カウントがゼロになったら関数を呼び出すことです。
まだない場合は、次のようなクラスを自分で作成します。
class countdown_function {
public:
countdown_function( size_t count );
countdown_function( const countdown_function& ) = default;
countdown_function( countdown_function&& ) = default;
countdown_function& operator=( const countdown_function& ) = default;
countdown_function& operator=( countdown_function&& ) = default;
// Callback to be invoked
countdown_function& operator=(std::function<void()> callback);
countdown_function& operator--();
private:
struct internal {
std::function<void()> _callback;
size_t _count;
// + some concurrent handling
};
// Make sure this class can be copied but still references
// same state
std::shared_ptr<internal> _state;
};
似たようなものはすでにどこかで入手できますか?
シナリオは次のとおりです。
countdown_function counter( 2 );
counter = [success_callback]() {
success_callback();
};
startTask1Async( [counter, somework]() {
somework();
--counter;
}, errorCallback );
startTask2Async( [counter, otherwork]() {
otherwork();
--counter;
}, errorCallback );