gpt4 book ai didi

java - CountDownLatch 等价物

转载 作者:可可西里 更新时间:2023-11-01 18:37:54 26 4
gpt4 key购买 nike

对于某些并发编程,我可以使用 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 );

最佳答案

有一个proposal涵盖下一个 C++ 标准。一个实现作为 google concurrency library 的一部分可用.

关于java - CountDownLatch 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15717289/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com