C++ mutex in namespace std does not name a type(命名空间 std 中的 C++ 互斥锁未命名类型)
问题描述
我正在编写一个简单的 C++ 程序来演示锁的使用.我正在使用 codeblocks
和 gnu
gcc
编译器.
I'm writing a simple C++ program to demonstrate the use of locks. I am using codeblocks
and gnu
gcc
compiler.
#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;
}
我收到以下错误:命名空间 std 中的互斥锁没有命名类型
.
为什么我会收到这个错误?编译器不支持使用锁吗?
Why am I getting this error? Doesn't the compiler support use of locks?
推荐答案
我碰巧遇到了同样的问题.GCC 在 Linux 下与 std::mutex
一起工作得很好.但是,在 Windows 上情况似乎更糟.在 <mutex>MinGW GCC 4.7.2 附带的头文件(我相信你也在使用 MinGW GCC 版本),我发现互斥类是在以下 #if
保护下定义的:
I happened to be looking at the same problem. GCC works fine with std::mutex
under 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 #if
guard:
#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
遗憾的是,_GLIBCXX_HAS_GTHREADS
没有在 Windows 上定义.运行时支持根本不存在.
Regretfully, _GLIBCXX_HAS_GTHREADS
is not defined on Windows. The runtime support is simply not there.
您可能还想直接在 MinGW 邮件列表上提问,以防某些 GCC 专家可以帮助您.
You may also want to ask questions directly on the MinGW mailing list, in case some GCC gurus may help you out.
MinGW-w64 项目提供了必要的运行时支持.查看 http://mingw-w64.sourceforge.net/ 和 https://sourceforge.net/projects/mingw-w64/files/.另外,正如 0xC0000022L 指出的,您需要下载 POSIX 线程版本(我上次没提到).
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).
这篇关于命名空间 std 中的 C++ 互斥锁未命名类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:命名空间 std 中的 C++ 互斥锁未命名类型


基础教程推荐
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01