mercredi 1 avril 2015

C++11 doc defines std::atomic::fetch_or() and std::atomic::fetch_and() only for Integral types. In this way, MSVC++ 2012 std::atomic< bool > is not implements this functions. Does anyone know why?


I found only this solution. Implement specialization for std::atomic_fetch_or and std::atomic_fetch_or.



namespace std
{
template<>
inline bool atomic_fetch_or<bool>(std::atomic<bool>* atom, bool val)
{
bool bRes = !val;
atom->compare_exchange_strong(bRes, true);
return bRes;
}

template<>
inline bool atomic_fetch_or_explicit<bool>(std::atomic<bool>* atom, bool val, std::memory_order order)
{
bool bRes = !val;
atom->compare_exchange_strong(bRes, true, order);
return bRes;
}


template<>
inline bool atomic_fetch_and<bool>(std::atomic<bool>* atom, bool val)
{
bool bRes = true;
atom->compare_exchange_strong(bRes, val);
return bRes;
}

template<>
inline bool atomic_fetch_and_explicit<bool>(std::atomic<bool>* atom, bool val, std::memory_order order)
{
bool bRes = true;
atom->compare_exchange_strong(bRes, val, order);
return bRes;
}
}


Unfortunately it is inconvenient to use as if it were a built-in operators.


Aucun commentaire:

Enregistrer un commentaire