I'm trying to implement a spin lock using an atomic_flag. I know that with C++11 I have to initialise the atomic_flag variable, but I can't get it to compile. My code looks like this:
class SpinLock
{
public:
SpinLock()
:m_flag(ATOMIC_FLAG_INIT) /// syntax error : missing ')' before '{'
{
}
void lock()
{
while (m_flag.test_and_set() == true){}
}
void unlock()
{
m_flag.clear();
}
private:
SpinLock &operator=(const SpinLock &);
private:
std::atomic_flag m_flag;
};
When I compile the code I get 'syntax error : missing ')' before '{''. I also see that ATOMIC_FLAG_INIT is defined as {0}, but what is the correct way to write this then?
The following compiles, but is it still threads safe?
SpinLock()
{
m_flag.clear();
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…