有没有办法在C++中制作无锁"counter"随机访问迭代器?
Is there a way of making a lock-free "counter" random access iterator in C++?
我正在试验一个玩具问题的执行策略,即在给定其系数和评估点 (x) 的情况下,在某个点评估多项式。
这是我的实现:
class counter: public std::iterator<
std::random_access_iterator_tag, // iterator_category
size_t, // value_type
size_t, // difference_type
const size_t*, // pointer
size_t // reference
>{
size_t num = 0;
public:
explicit counter(size_t _num) : num(_num) {}
counter& operator++() {num += 1; return *this;}
counter operator++(int) {counter retval = *this; ++(*this); return retval;}
bool operator==(counter other) const {return num == other.num;}
bool operator!=(counter other) const {return !(*this == other);}
counter& operator+=(size_t i) { num += i; return *this; }
counter& operator-=(size_t i) { num -= i; return *this; }
counter operator +(counter &other) const { return counter(num + other.num);}
counter operator -(counter &other) const { return counter(num - other.num); }
counter operator +(size_t i) const { return counter(num + i); }
counter operator -(size_t i) const {return counter(num - i); }
reference operator*() const {return num;}
};
double better_algorithm_polinomials(const vector<double> & coeffs, double x) {
return transform_reduce(execution::par, cbegin(coeffs), end(coeffs), counter(0), 0.0, plus{}, [x](double coeff, size_t index) { return coeff * pow<double>(x, index); });
}
这适用于 par 策略,但对于par_unseq,由于竞争条件,这失败了。
我试图使用 atomic_size_t 来缓解它们,但有些地方(例如复制构造或 ++(int) 运算符),我不是原子的,可能必须使用锁......我想知道是否有更好的方法。
这不起作用:
class counter: public std::iterator<
std::random_access_iterator_tag, // iterator_category
atomic_size_t, // value_type
atomic_size_t, // difference_type
const atomic_size_t*, // pointer
atomic_size_t // reference
>{
atomic_size_t num = 0;
public:
explicit counter(size_t _num) : num(_num) {}
counter(counter &other) { num = other.num.load();}
counter& operator++() {num += 1; return *this;}
const counter operator++(int) {num += 1; return counter(num-1);}
bool operator==(counter &other) const {return num == other.num;}
bool operator!=(counter &other) const {return !(*this == other);}
counter& operator+=(size_t i) { num += i; return *this; }
counter& operator-=(size_t i) { num -= i; return *this; }
counter operator +(counter &other) const { return counter(num + other.num);}
difference_type operator -(counter &other) const { return num - other.num; }
counter operator +(size_t i) const { return counter(num + i); }
difference_type operator -(size_t i) const {return num - i; }
size_t operator [](size_t i) const {return i;}
reference operator*() const {return num.load();}
};
我试图使用atomic_size_t来缓解它们
请注意,您应该静态断言std::atomic<std::size_t>
在您的平台中实际上是无锁的。
但是有些地方(例如复制构造或++(int)运算符),我不是原子的,可能必须使用锁...
使用非顺序执行策略时无法获取锁!参见[算法.parallel],例如这个和这个。
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 为什么 Serial.println(<char[]>);返回随机字符?
- 字符串-C++后显示的随机字符
- 循环中的随机函数
- 在c++构造函数中使用随机字符串生成器
- 使用std::mt19937从字符串中返回一个随机单词
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- 如何在C++中高效地构造随机骰子
- 在类中使用随机生成器时出现性能问题
- 在将数字随机生成为数组期间从内存输出随机数的数组
- 将字符随机转换为大写的函数
- 为什么 vector 的随机访问迭代器给出与指针不同的内存地址?
- 如何生成一个随机的 n 位数,其中 n 是任意的
- 将随机生成的数字添加到数组 + 对这些数组求平均值
- 如何使用要传递给 mt19937 的可选随机种子参数设计函数
- 在C++中随机生成 20 个非重复数字
- GCC:随机构建导致执行期间分段错误
- 如何使用 SML 随机生成八进制元组
- 当我尝试使用它时,Scanf 会抛出一个随机异常(scanf_s 也是如此)
- 有没有办法在C++中制作无锁"counter"随机访问迭代器?