我正在尝试使用atomic_flag实现自旋锁。我知道使用C ++ 11我必须初始化atomic_flag变量,但我无法编译它。我的代码如下所示:
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;
};
当我编译代码时,我得到'语法错误:缺少')''''''之前。我也看到ATOMIC_FLAG_INIT定义为{0},但是这样写的正确方法是什么呢?
以下编译,但它仍然是线程安全吗?
SpinLock()
{
m_flag.clear();
}
Visual Studio 2012不支持c ++ 11初始化列表(请参阅c ++ 11支持页面)
但是在Visual Studio 2013中支持它(请参阅中的“initializer_list构造函数”部分) 统一初始化文档)
同时在您的情况下,构造函数可以只使用赋值 m_flag = ATOMIC_FLAG_INIT;
更新:
它似乎没有测试上面的任务,而是使用 m_flag.clear();
达到了相同的效果
它看起来像一个bug(视觉2013 rtm)。 ATOMIC_FLAG_INIT
是特定于实现并解决为宏 {0}
。这意味着Microsoft使用聚合规则来完成工作。
引用cppreference关于它们: Until C++11, aggregate initialization could not be used in a constructor initializer list due to syntax restrictions.
。我得出结论,微软尚未改变这种行为。
这里有一个例子在clang上运行正常并且在VS2013 RTM上失败了一个更简单的情况:
struct Pod {
int m_val;
};
Pod g_1{ 0 }; // aggregate initialization
Pod g_2{ { 0 } }; // just like ATOMIC_FLAG_INIT
struct Foo {
Foo() : m_2 { 0 } {} // error C2664: 'Pod::Pod(const Pod &)' : cannot convert argument 1 from 'int' to 'const Pod &'
Pod m_1{ 0 }; // error C2664: 'Pod::Pod(const Pod &)' : cannot convert argument 1 from 'int' to 'const Pod &'
Pod m_2; // ok
};