【发布时间】:2014-05-06 05:49:25
【问题描述】:
这是用于 TBB 代码的原子计数器类。
#include <atomic>
template <typename T = int>
struct AtomicCounter {
private:
std::atomic<T> value;
std::atomic<T> num_inter;
public:
void put_inter(T niter) {
T x = num_inter.fetch_add(niter);
}
T get_inter() { return num_inter.load(); }
void increment() { ++value; }
void put(T data) { value.fetch_add(data) ; // ignore the old value returned }
void decrement() { --value; }
T get() { return value.load(); }
};
我正在使用并行 foreach 循环,其中每个线程都有自己的本地计数器值,并使用 AtomicCounter 类的 put 函数更新全局计数器对象。并行 foreach 的函数对象将此全局计数器作为参考。
template<typename counter_t>
struct my_functor {
public:
my_functor(){}
my_functor(counter_t &c):m_c(c){ }
template<typename label_t>
void operator()(label_t label) const {
// do some work --> local counter
m_c.put(local_value); // put the local counter value in the global counter
}
private:
counter_t& m_c;
mutable int local_value; //local counter value
}
// for TBB
//declare an object of this class as a global counter.
AtmoicCounter<int> c;
my_functor<atomicCounter<int>> func(c) //functor object
parallel_for_each( mywork.begin(), mywork.end(), func) //TBB parallel for each
所以基本上,每个线程都会更新全局计数器。我声明 std::atomic 成员的 atomicCounter 类有什么问题吗?我正在使用具有 C++11 功能和英特尔 TBB 4.2 的 GCC 4.7.2
谢谢!
【问题讨论】:
-
您是在问您的 实现是否正确,GCC 的原子实现是否正确? (如果是后者,您遇到了什么问题?)
-
@Mat- 我刚刚在某处读到 GCC 4.7 中的原子实现与 GCC 4.8 中的不同之处在于对无锁指令的支持。另外,我想确认一下我使用原子计数器保持全局计数器值的方式是否正确?
-
您正在将 reduce 操作打包成普通的
for_each。相反,您应该使用适当的 a reduce 函数(xxx::parallel_reduce)并完全避免使用原子。
标签: c++ multithreading c++11 tbb reduction