我正在尝试使用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;
};
Run Code Online (Sandbox Code Playgroud)
当我编译代码时,我会在'{''之前得到'语法错误:缺失')'.我也看到ATOMIC_FLAG_INIT定义为{0},但是这样写的正确方法是什么呢?
以下编译,但它仍然是线程安全吗?
SpinLock()
{
m_flag.clear();
}
Run Code Online (Sandbox Code Playgroud) 我有这门课:
class Taxi {
Wheel myWheel[4];
public:
Taxi();
};
Run Code Online (Sandbox Code Playgroud)
和Wheel是另一类包含:
class Wheel{
int radius,
tickness;
public:
Wheel(int,int);
};
Run Code Online (Sandbox Code Playgroud)
现在,我想要做的是在Taxi构造函数的初始化列表中初始化"myWheel [4]",如下所示:
Taxi::Taxi () :Wheel[0](5,5), Wheel[1](3,3), Wheel[2](5,5), Wheel[3](3,3) {
cout << "Ctor of Taxi" << endl;
}
Run Code Online (Sandbox Code Playgroud)
但它不起作用,我真的需要一些帮助,谢谢:)