【问题标题】:What should i have on windows to use std::mutex?我应该在 Windows 上使用什么 std::mutex?
【发布时间】:2020-07-02 16:10:22
【问题描述】:

在尝试使用 C++ std::mutex 时,我在 Windows 10 64 位上遇到错误。该代码基本上是为 Linux 编写的,但我正在尝试将其移植到 Windows。 (您可以在我下面添加的错误消息中看到编译器行。)

这是我的代码:

#ifndef UNTITLED_LIBRARY_H
#define UNTITLED_LIBRARY_H

#include <sys/types.h>
#include <Winsock2.h>
#include <mutex>
#include <thread>

class TCPServer
{
    static std::mutex mt;
};

#endif //UNTITLED_LIBRARY_H
Here is the error message:

g++ -Wall -std=c++14  -I./ library.h  -o libSimpleNetwork.so -fPIC -shared
library.h:11:17: error: 'mutex' in namespace 'std' does not name a type
   11 |     static std::mutex mt;
      |                 ^~~~~
library.h:8:1: note: 'std::mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
    7 | #include <thread>
  +++ |+#include <mutex>
    8 |


【问题讨论】:

  • 没什么。尽管互斥锁是特定于操作系统的,但调用适当的操作系统结构是实现的工作,而不是程序员的工作。为什么这样问?您是否尝试过某事并遇到错误?
  • 就用吧。有什么问题?如果您有一个请提供您收到的代码和确切的错误文本
  • 请详细说明“清单”、“使用”、“标志”的含义。目前任何回答你问题让你满意的人都会因为他们的千里眼而让我惊叹不已。
  • 我可以猜到 - 在 Windows 上使用了特定于 Linux 的标头
  • 抱歉,这里一团糟。你包括unistd.h Winsock2.h pthread.h sys/types.h,此时&lt;mutex&gt;真的是最少的你的问题......也许退后几步。

标签: c++ windows-10


【解决方案1】:

所以这个问题的答案是安装mingw64(不是mingw32!):https://sourceforge.net/projects/mingw-w64/

这包括 gcc 对 libstd 所需的 posix x86_64-posix-seh

【讨论】:

  • 另外,如果在运行 gcc -v 时这是正确的,你应该会看到“Thread model: posix”
猜你喜欢
  • 2017-03-28
  • 2014-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-07
  • 1970-01-01
  • 2017-07-24
  • 2012-06-06
相关资源
最近更新 更多