Handling errno is one of the struggle of using POSIX API over a multithreaded environment. Would it be reasonable to use a lock using std::atomic
like the following one?
class FastLock{
std::atomic_int value;
public:
FastLock()
: value{0}{}
void unlock()
{
value.store(0,std::memory_order_release);
}
bool try_lock()
{
int r = value.exchange(1,std::memory_order_acquire);
return !r;
}
};
The context would be similar to this:
template<typename function, typename ...args>
auto shield(function _fn){
static FastLock* lk = new FastLock{};
return [=](args... _v){
while(lk->try_lock());
auto ret = std::forward(_fn, _v...);
auto errval = errno;
lk->unlock();
return std::make_pair(ret,errval);
};
}
Would this result in any sort of either undefined behaviour or implementation defined behaviour?