C语言 Visual Studio 2017,如何使 #include <pthread.h> 工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48894212/
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-09-02 10:44:05  来源:igfitidea点击:

Visual Studio 2017, How to make #include <pthread.h> work?

cvisual-studio-2017

提问by Jbags

I feel like I've been searching for hours today on how to make #include <pthread.h>work in Visual Studio 2017 on Windows 10 and haven't come across an answer that has worked. Can someone please instruct me how to get this to work?

我觉得我今天一直在寻找关于如何#include <pthread.h>在 Windows 10 上的 Visual Studio 2017 中工作的几个小时,但还没有找到有效的答案。有人可以请教我如何让它工作吗?

The error I receive is: cannot open source file "pthread.h"

我收到的错误是:无法打开源文件“pthread.h”

采纳答案by nemequ

Windows doesn't natively support pthreads.

Windows 本身不支持 pthread。

There is an implementation of pthreads for Windowsyou could use. If you're just trying to get some code running on Windows ASAP it may be your best bet.

并行线程的Windows的实现,你可以使用。如果您只是想尽快在 Windows 上运行一些代码,那么这可能是您最好的选择。

If you can modify the code, you might want to take a look at what pthread functions you are actually using; if all you need is a mutex a few #ifdefs around pthreads/winapi may be a better way to go.

如果您可以修改代码,您可能想看看您实际使用的 pthread 函数;如果您只需要一个互斥锁,那么 pthreads/winapi 周围的一些 #ifdefs 可能是更好的方法。

There are also some projects which implement compatibility layers that aren't as expressive as the full pthreads API, but are more than enough for most projects. TinyCThreadis one option (which I maintain, so take the recommendation with a grain of salt) which implements the C11 threads API.

还有一些项目实现了兼容层,它们不像完整的 pthreads API 那样具有表现力,但对于大多数项目来说已经足够了。TinyCThread是一个实现 C11 线程 API 的选项(我维护它,所以请保留一些建议)。

Other options include:

其他选项包括:

OpenMPcould also be a good choice. It is really easy to use, but operates at a higher level and so it may require a more substantial rewrite.

OpenMP也可能是一个不错的选择。它真的很容易使用,但在更高级别上运行,因此可能需要更大量的重写。

回答by Bala Ganesh

In Visual Studio 2017

在 Visual Studio 2017 中

  1. Project -> Manage Nuget Packages

  2. In browse Tab search for 'pthread'

  3. Select Install[Search Results in Nuget packages]

  1. 项目 -> 管理 Nuget 包

  2. 在浏览标签中搜索“pthread”

  3. 选择安装[Nuget包中的搜索结果]

enter image description here

在此处输入图片说明

回答by Manoj M

For Visual Studio 2017, I've installed through NuGet Packages.

对于 Visual Studio 2017,我是通过 NuGet 包安装的。

Try below steps,

尝试以下步骤,

  1. Go to Project > 'Manage NuGet Packages'
  2. Browse > search for 'pthread' > install
  1. 转到项目>“管理 NuGet 包”
  2. 浏览 > 搜索“pthread” > 安装

回答by chrset

If you have vcpkginstalled, then you can just do:

如果您安装了vcpkg,那么您可以执行以下操作:

vcpkg.exe install pthread

That will download some code from https://sourceforge.net/projects/pthreads4w, compile it and install it in your system. If vcpkgis setup correctly on your machine, Visual Studio should use these files automatically.

这将从https://sourceforge.net/projects/pthreads4w下载一些代码,编译它并将其安装到您的系统中。如果您的计算机上正确设置了vcpkg,Visual Studio 应自动使用这些文件。