tbb::atomic<bool>?

tbb::atomic<bool>?

Are atomic boolean values supported in tbb::atomic?

I have seen an old blog post that mentions this would be added (see here).

4 帖子 / 0 全新
最新文章
如需更全面地了解编译器优化,请参阅优化注意事项

It has not been added yet. Adding it to "tbb/atomic.h" should be a matter of cloning the partial specialization for atomic. The hard work (really just tedious) is updating the unit test/test_atomic.cpp to make sure it works.

- Arch

I added atomic to the development version, so it will show up in the next development release. It was almost a clone of atomic.To shut up gratuitous warnings from a compiler, I added a few !=0 tests to explicitly convert integral types to boolean. The warning was about a performance issue. I consider it gratuitous because modern architectures typically have instructions for converting integers to bools without branching. Attached is the definition of atomic that I added to include/tbb/atomic.h, inside namespace tbb.

template<>
struct atomic {
private:
bool volatile my_value;
public:
typedef bool value_type;
template
value_type compare_and_swap( value_type value, value_type comparand ) {
return internal::atomic_traits::compare_and_swap(&my_value,internal::intptr(value),internal::intptr(comparand))!=0;
}
 value_type compare_and_swap( value_type value, value_type comparand ) {
return compare_and_swap<__TBB_full_fence>(value,comparand);
}
 template
value_type fetch_and_store( value_type value ) {
return internal::atomic_traits::fetch_and_store(&my_value,internal::intptr(value))!=0;
}
 value_type fetch_and_store( value_type value ) {
return fetch_and_store<__TBB_full_fence>(value);
}
 operator value_type() const {
return __TBB_load_with_acquire(my_value);
}
 value_type operator=( value_type rhs ) {
__TBB_store_with_release(my_value,rhs);
return rhs;
}
};

Cool. I'll test it out.

发表评论

登录添加评论。还不是成员?立即加入