命名空间 std 中的 C++ 互斥锁未命名类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14191566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:06:38  来源:igfitidea点击:

C++ mutex in namespace std does not name a type

c++multithreadingcompiler-errorslockingmingw

提问by arjun

I'm writing a simple C++ program to demonstrate the use of locks. I am using codeblocksand gnugcccompiler.

我正在编写一个简单的 C++ 程序来演示锁的使用。我正在使用codeblocksgnugcc编译器。

 #include <iostream>
 #include <thread>
 #include <mutex>
 using namespace std;
 int x = 0; // shared variable

 void synchronized_procedure()
 {
    static std::mutex m;
    m.lock();
    x = x + 1;
    if (x < 5)
    {
       cout<<"hello";
    }
    m.unlock();

 }

int main()
{

   synchronized_procedure();
   x=x+2;
   cout<<"x is"<<x;
}

I'm getting the following error: mutex in namespace std does not name a type.

我收到以下错误:mutex in namespace std does not name a type.

Why am I getting this error? Doesn't the compiler support use of locks?

为什么我收到这个错误?编译器不支持使用锁吗?

回答by Yongwei Wu

I happened to be looking at the same problem. GCC works fine with std::mutexunder Linux. However, on Windows things seem to be worse. In the <mutex> header file shipped with MinGW GCC 4.7.2 (I believe you are using a MinGW GCC version too), I have found that the mutex class is defined under the following #ifguard:

我碰巧在看同样的问题。GCCstd::mutex在 Linux 下运行良好。但是,在 Windows 上,情况似乎更糟。在 MinGW GCC 4.7.2 附带的 <mutex> 头文件中(我相信你也在使用 MinGW GCC 版本),我发现 mutex 类是在以下#if保护下定义的:

#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)

Regretfully, _GLIBCXX_HAS_GTHREADSis not defined on Windows. The runtime support is simply not there.

遗憾的是,_GLIBCXX_HAS_GTHREADS在 Windows 上没有定义。运行时支持根本不存在。

You may also want to ask questions directly on the MinGW mailing list, in case some GCC gurus may help you out.

您可能还想直接在 MinGW 邮件列表上提问,以防某些 GCC 专家可能会帮助您。

EDIT: The MinGW-w64 projects provides the necessary runtime support. Check out http://mingw-w64.sourceforge.net/and https://sourceforge.net/projects/mingw-w64/files/. Also, as 0xC0000022L pointed out, you need to download the POSIX thread version (I missed mentioning it last time).

编辑:MinGW-w64 项目提供了必要的运行时支持。查看http://mingw-w64.sourceforge.net/https://sourceforge.net/projects/mingw-w64/files/。另外,正如 0xC0000022L 所指出的,您需要下载 POSIX 线程版本(我上次没提到)。

回答by IsakBosman

This has now been included in MingW (Version 2013072300). To include it you have to select the pthreads package in the MinGW Installation Manager.

这现已包含在 MingW(版本 2013072300)中。要包含它,您必须在 MinGW 安装管理器中选择 pthreads 包。

Pthreads package options from MingW Installation Manager

MingW 安装管理器中的 Pthreads 包选项

回答by Amir Saniyan

Use POSIX threading model for MINGW:

对 MINGW 使用 POSIX 线程模型:

$ sudo update-alternatives --config i686-w64-mingw32-gcc
<choose i686-w64-mingw32-gcc-posix from the list>

$ sudo update-alternatives --config i686-w64-mingw32-g++
<choose i686-w64-mingw32-g++-posix from the list>

$ sudo update-alternatives --config x86_64-w64-mingw32-gcc
<choose x86_64-w64-mingw32-gcc-posix from the list>

$ sudo update-alternatives --config x86_64-w64-mingw32-g++
<choose x86_64-w64-mingw32-g++-posix from the list>

See also: mingw-w64 threads: posix vs win32

另请参阅:mingw-w64 线程:posix 与 win32

回答by Walter Waldo

Mutex, at least, is not supported in 'Thread model: win32' of the Mingw-builds toolchains. You must select any of the toolchains with 'Thread model: posix'. After trying with several versions and revisions (both architectures i686 and x86_64) I only found support in x86_64-4.9.2-posix-seh-rt_v3-rev1 being the thread model, IMO, the determining factor.

至少,Mingw-builds 工具链的“线程模型:win32”不支持互斥锁。您必须选择任何带有“线程模型:posix”的工具链。在尝试了多个版本和修订版(架构 i686 和 x86_64)后,我只发现 x86_64-4.9.2-posix-seh-rt_v3-rev1 中的支持是线程模型,IMO,决定因素。

回答by vallismortis

I encountered this same problem when using MingW-W64 7.2.0. I tested out several different Windows builds from the mingw-64 download page, and found that MinGW-W64 GCC-8.1.0supports mutexand contains the pthreadlibrary. When installing, I selected the following options:

我在使用 MingW-W64 7.2.0 时遇到了同样的问题。我从mingw-64 下载页面测试了几个不同的 Windows 版本,发现MinGW-W64 GCC-8.1.0支持mutex并包含该pthread库。安装时,我选择了以下选项:

  • x86_64
  • posix
  • seh
  • x86_64
  • posix

My multi-threaded code based on pthreadsnow compiles and runs cleanly on both Windows and Linux with no changes.

我基于的多线程代码pthreads现在可以在 Windows 和 Linux上编译和运行,无需任何更改。

Installed MingW version on Windows 8.1

在 Windows 8.1 上安装 MingW 版本

This version is leaner than the 7.3.0 build I was using because it doesn't have a CygWin environment or package manager. I also copied mingw32-make.exeto make.exeso my Makefile wouldn't need to be modified. The installer creates a "Run terminal" link in the Windows Start Menu.

这个版本比我使用的 7.3.0 版本更精简,因为它没有 CygWin 环境或包管理器。我也复制mingw32-make.exemake.exe这样我的 Makefile 就不需要修改了。安装程序会在 Windows 开始菜单中创建一个“运行终端”链接。

Building and running pthread application on Windows 8.1 using MingW

使用 MingW 在 Windows 8.1 上构建和运行 pthread 应用程序

回答by Doan Quang Viet

I got the same error with gcc4.7.7.

我在 gcc4.7.7 上遇到了同样的错误。

After adding "-std=c++0x", it is fixed.

添加“-std=c++0x”后,修复。

回答by Bastienm

I don't know if it works for everybody, but in other way you just have to update your ndk. I'm using ndk-r11c and it works perfectly.

我不知道它是否适用于每个人,但在其他方面你只需要更新你的 ndk。我正在使用 ndk-r11c,它运行良好。

回答by TungBui

I am facing this error today, and fixed it by following steps:

我今天面临这个错误,并通过以下步骤修复它:

  • Project > Build option...
  • The default selected compiler: GNU GCC Compiler
  • On tab "Compiler settings / Compiler flags", check option "Have g++ follow the C++11 ISO C++ language standard [-std=c++11]"
  • 项目 > 构建选项...
  • 默认选择的编译器:GNU GCC Compiler
  • 在选项卡“编译器设置/编译器标志”上,选中选项“让 g++ 遵循 C++11 ISO C++ 语言标准 [-std=c++11]”

Good luck.

祝你好运。

回答by Nan Shi

my gcc version is 5.4 and i solved this problem when adding #include and also adding -std=c++11 at my CmakeLists.txt as below:

我的 gcc 版本是 5.4,我在添加 #include 并在我的 CmakeLists.txt 中添加 -std=c++11 时解决了这个问题,如下所示:

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall  -O3 -march=native -std=c++11")

回答by Kolyunya

Many classes of the standard thread library can be replaced with the boost ones. A very easy workaround is to change the entire standard mutexfile with a couple of lines.

标准线程库的许多类都可以用 boost 类替换。一个非常简单的解决方法是mutex用几行更改整个标准文件。

#include <boost/thread.hpp>

namespace std
{
   using boost::mutex;
   using boost::recursive_mutex;
   using boost::lock_guard;
   using boost::condition_variable;
   using boost::unique_lock;
   using boost::thread;
}

And do not forget to link against boost thread library.

并且不要忘记链接到 boost 线程库。