Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.
Announcements
This community is designed for sharing of public information. Please do not share Intel or third-party confidential information here.

tbb::atomic<bool>?

nate3
Beginner
316 Views
Are atomic boolean values supported in tbb::atomic?

I have seen an old blog post that mentions this would be added (see here).
0 Kudos
3 Replies
ARCH_R_Intel
Employee
316 Views

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

ARCH_R_Intel
Employee
316 Views

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;
}
};
Intel_C_3
Beginner
316 Views
Cool. I'll test it out.
Reply