C++ 在 Windows 构建中使用 pthread.h

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

Using pthread.h on a windows build

c++cpthreads

提问by oldspice

I have a codebase that makes extensive use of pthread.h. On a windows visual studio project, this obviously doesn't work since pthreads is a unix utility.

我有一个广泛使用 pthread.h 的代码库。在 Windows Visual Studio 项目中,这显然不起作用,因为 pthreads 是一个 unix 实用程序。

I know there exists pthread-win32 or something of the sort, but is there a way to have a codebase use this, without replacing all of the pthread code?

我知道存在 pthread-win32 或类似的东西,但是有没有办法让代码库使用它,而无需替换所有 pthread 代码?

edit: I'd prefer to not have to go through and replace all the pthread calls. This is my question

编辑:我宁愿不必经历并替换所有 pthread 调用。这是我的问题

采纳答案by Novikov

Try http://sourceware.org/pthreads-win32/. While I've never used that particular library, I've had luck using some windows ports/abstraction layers of POSIX APIs.

试试http://sourceware.org/pthreads-win32/。虽然我从未使用过那个特定的库,但我很幸运地使用了 POSIX API 的一些 Windows 端口/抽象层。

回答by bmargulies

I would recommend boost as a library that contains a platform-independent thread abstraction. I don't think you'll find any attempt to call the pthread functions themselves to be satisfying. It's also not very hard to map pthread to Win32 for yourself.

我会推荐 boost 作为一个包含独立于平台的线程抽象的库。我认为您不会发现任何调用 pthread 函数本身的尝试都令人满意。自己将 pthread 映射到 Win32 也不是很难。

回答by Adam Rosenfield

If the code is very heavily *nix-based, you'll probably have the best luck compiling it with Cygwin+GCC. If pthreads is the only *nix dependency and you'd like to avoid the Cygwin dependency, then you should go with Pthreads-win32.

如果代码非常基于 *nix,那么使用Cygwin+GCC编译它可能会很幸运。如果 pthreads 是唯一的 *nix 依赖项并且您想避免 Cygwin 依赖项,那么您应该使用Pthreads-win32