对于一些并发编程,我可以使用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 );
shared_ptr
的释放器并丢弃手动减量。 - Xeoerror
)回调,在这种情况下,我不希望调用success_callback
。这就是我没有使用shared_ptr的删除器的原因。 - abergmeier