简体   繁体   English

冲突声明 std::lock_guard<std::mutex></std::mutex>

[英]conflicting declaration std::lock_guard<std::mutex>

I don't understand why the following code returns an error.我不明白为什么下面的代码会返回错误。

#include <mutex>

int main() {
    std::mutex mtx;
    std::lock_guard<std::mutex> (mtx);
}

lock.cpp: In function 'int main()': lock.cpp:在 function 'int main()' 中:

lock.cpp:5:37: error: conflicting declaration 'std::lock_guardstd::mutex mtx' std::lock_guardstd::mutex (mtx); lock.cpp:5:37:错误:冲突声明 'std::lock_guardstd::mutex mtx' std::lock_guardstd::mutex (mtx);

lock.cpp:4:16: note: previous declaration as 'std::mutex mtx' std::mutex mtx; lock.cpp:4:16:注意:先前的声明为 'std::mutex mtx' std::mutex mtx;

But, the following code compiles correctly.但是,以下代码编译正确。

class Test {
public:
    void lock() {
        std::lock_guard<std::mutex> (this->mtx_);
        std::lock_guard<std::mutex> (this->mtx_);
    }

private:
    std::mutex mtx_;
};

But, the following code is not ok.但是,下面的代码不行。

int main() {
    std::lock_guard<std::mutex> (mtx);
    std::lock_guard<std::mutex> (mtx);
}

You declared the variable mtx twice您两次声明了变量mtx

std::mutex mtx;
std::lock_guard<std::mutex> (mtx);

The first declaration may be equivalently rewritten like第一个声明可以等效地重写为

std::mutex ( mtx );
std::lock_guard<std::mutex> (mtx);

Or the second declaration may be rewritten like或者第二个声明可以重写为

std::mutex mtx;
std::lock_guard<std::mutex> mtx;

That is you may enclose a declarator in parentheses.也就是说,您可以将声明符括在括号中。

Thus the identifier mtx is declared twice with different types.因此,标识符mtx以不同的类型声明了两次。

It seems you forgot to specify an identifier in the second declaration.:) Something like您似乎忘记在第二个声明中指定标识符。:) 类似

std::lock_guard<std::mutex> my_lock( mtx );

EDIT: After you updated your question with a new code snippet编辑:在你用新的代码片段更新你的问题之后

class Test {
public:
    void lock() {
        std::lock_guard<std::mutex> (this->mtx_);
        std::lock_guard<std::mutex> (this->mtx_);
    }

private:
    std::mutex mtx_;
};

then these records那么这些记录

std::lock_guard<std::mutex> (this->mtx_);
std::lock_guard<std::mutex> (this->mtx_);

are not declarations.不是声明。 They are expressions that use the data member (an expression of accessing a data member) this->mtx_ .它们是使用数据成员(访问数据成员的表达式) this->mtx_的表达式。 That is the expression this->mtx_ is not an identifier.这就是表达式this->mtx_不是标识符。

Finding out the problem:找出问题所在:

In first example:在第一个例子中:

std::mutex mtx;
std::lock_guard<std::mutex> (mtx);

Here you have declared a variable mtx of type std::mutex .在这里,您声明了一个std::mutex类型的变量mtx The next line is ambiguous.下一行是模棱两可的。 It can be treated as an expression with declaring a variable of type std::lock_guard<std::mutex> , but since you do not passed a parameter to lock_guard constructor, it is being treated as a pure declaration with syntax *type* (*identifier*) .它可以被视为一个声明std::lock_guard<std::mutex>类型变量的表达式,但由于您没有将参数传递给 lock_guard 构造函数,因此它被视为具有语法*type* (*identifier*) You can find more about resolving ambiguous here .您可以在此处找到有关解决歧义的更多信息。

In second example:在第二个例子中:

void lock() {
    std::lock_guard<std::mutex> (this->mtx_);
    std::lock_guard<std::mutex> (this->mtx_);
}

You are trying to use syntax *type* *identifier* (*constructor arguments*) , and pass an already existent member mtx_ of your class and there is no variable name.您正在尝试使用语法*type* *identifier* (*constructor arguments*) ,并传递您的 class 已经存在的成员 mtx_ 并且没有变量名。 There is no ambiguity and no errors because of absent variable name.由于缺少变量名,因此没有歧义和错误。 It is just to declaration of anonymous variables.它只是声明匿名变量。

The third example is definitely not ok because your just repeating the mistake from ex.第三个例子肯定不行,因为你只是重复前任的错误。 1 twice, by declaring two variables named mtx . 1 两次,通过声明两个名为mtx的变量。

int main() {
    std::lock_guard<std::mutex> (mtx);
    std::lock_guard<std::mutex> (mtx);
}

How to resolve:如何解决:

As I already mentioned in comment, you should better use a name for your lock.正如我在评论中已经提到的,你最好为你的锁使用一个名字。 Either it can be要么它可以

std::lock_guard<std::mutex> lock(mtx);

, or if like using parentheses to distract yourself ,或者如果喜欢用括号分散自己的注意力

std::lock_guard<std::mutex>(lock)(mtx);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM