Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.

tbb::atomic<bool>?

nate3
Beginner
857 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
857 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

0 Kudos
ARCH_R_Intel
Employee
857 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;
}
};
0 Kudos
Intel_C_3
Beginner
857 Views
Cool. I'll test it out.
0 Kudos
Reply