C语言 我可以让 Unix 的 pthread.h 在 Windows 中编译吗?

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

Can I get Unix's pthread.h to compile in Windows?

cwindowsmultithreadingpthreads

提问by naspinski

If I try to compile a program with

如果我尝试编译一个程序

#include <pthread.h>

in it, I get the error:

在其中,我收到错误消息:

pthread.h: No such file or directory

Is it possible to get this to compile in a Windows environment?

是否有可能让它在 Windows 环境中编译?

I am using Vista with the latest MinGW.

我正在使用带有最新 MinGW 的 Vista。

I do not want to use the Microsoft Windows Services for UNIX Version 3.5 as I will have to move this to a Unix environment.

我不想使用 UNIX 版本 3.5 的 Microsoft Windows 服务,因为我必须将其移至 Unix 环境。

采纳答案by naspinski

pthread.his a header for the Unix/Linux (POSIX) API for threads. A POSIX layer such as Cygwin would probably compile an app with #include <pthreads.h>.

pthread.h是用于线程的 Unix/Linux (POSIX) API 的标头。诸如 Cygwin 之类的 POSIX 层可能会使用#include <pthreads.h>.

The native Windows threading API is exposed via #include <windows.h>and it works slightly differently to Linux's threading.

本机 Windows 线程 API 是通过公开的#include <windows.h>,它的工作方式与 Linux 的线程略有不同。

Still, there's a replacement "glue" library maintained at http://sourceware.org/pthreads-win32/; note that it has some slight incompatibilities with MinGW/VS (e.g. see here).

不过,在http://sourceware.org/pthreads-win32/ 上维护了一个替代的“胶水”库;请注意,它与 MinGW/VS 有一些轻微的不兼容(例如,请参见此处)。

回答by Randolpho

As @Ninefingers mentioned, pthreads are unix-only. Posix only, really.

正如@Ninefingers 所提到的,pthreads 仅限于 Unix。只有 Posix,真的。

That said, Microsoft does have a library that duplicates pthreads:

也就是说,微软确实有一个复制 pthread 的库:

Microsoft Windows Services for UNIX Version 3.5

适用于 UNIX 版本 3.5 的 Microsoft Windows 服务

Library Download

图书馆下载

回答by bobobobo

pthread.hisn't on Windows. But Windows has extensivethreading functionality, beginning with CreateThread.

pthread.h不在 Windows 上。但是 Windows 具有广泛的线程功能,从CreateThread开始。

My advice is don't get caught looking at WinAPI through the lens of another system's API. These systems are different. It's like insisting on riding the Win32 bike with your comfortable Linux bike seat. Well, the seat might not fit right and in some cases it'll just fall off.

我的建议是不要通过另一个系统的 API 来看待 WinAPI。这些系统是不同的。这就像坚持使用舒适的 Linux 自行车座椅骑 Win32 自行车。好吧,座椅可能不合适,在某些情况下它会掉下来。

Threads pretty much work the same on different systems, you have ThreadPoolsand mutexes. Having worked with both pthreadsand Windows threads, I can say the Windows threading offers quite a bit more functionality than pthreaddoes.

线程在不同系统上的工作方式几乎相同,您有ThreadPoolsmutexs。使用pthreadsWindows 线程和 Windows 线程后,我可以说 Windows 线程提供的功能要多得多pthread

Learning another API is pretty easy, just think in terms of the concepts (mutex, etc), then look up how to create one of those on MSDN.

学习另一个 API 非常容易,只需考虑概念(互斥锁等),然后在 MSDN 上查找如何创建其中一个。

回答by user2873330

Just pick up the TDM-GCC 64x package. (It constains both the 32 and 64 bit versions of the MinGW toolchain and comes within a neat installer.) More importantly, it contains something called the "winpthread" library.

只需拿起 TDM-GCC 64x 包。(它包含 32 位和 64 位版本的 MinGW 工具链,并包含在一个简洁的安装程序中。)更重要的是,它包含一个叫做“ winpthread”的库。

It comprises of the pthread.hheader, libwinpthread.a, libwinpthread.dll.astatic libraries for both 32-bit and 64-bit and the required .dlls libwinpthread-1.dlland libwinpthread_64-1.dll(this, as of 01-06-2016).

它包括的pthread.h报头,libwinpthread.alibwinpthread.dll.a对于32位和64位和所需的.dll静态库libwinpthread-1.dlllibwinpthread_64-1.dll(在此,作为2016年1月6日)。

You'll need to link to the libwinpthread.alibrary during build. Other than that, your code can be the same as for native Pthread code on Linux. I've so far successfully used it to compile a few basic Pthread programs in 64-bit on windows.

您需要libwinpthread.a在构建期间链接到库。除此之外,您的代码可以与 Linux 上的本机 Pthread 代码相同。到目前为止,我已经成功地使用它在 Windows 上以 64 位方式编译了一些基本的 Pthread 程序。

Alternatively, you can use the following library which wraps the windows threading API into the pthreads API: pthreads-win32.

或者,您可以使用以下库将 Windows 线程 API 包装到 pthreads API 中: pthreads-win32

The above two seem to be the most well known ways for this.

以上两种似乎是最广为人知的方式。

Hope this helps.

希望这可以帮助。

回答by Chris Becke

There are, as i recall, two distributions of the gnu toolchain for windows: mingw and cygwin.

我记得,windows 的 gnu 工具链有两个发行版:mingw 和 cygwin。

I'd expect cygwin work - a lot of effort has been made to make that a "stadard" posix environment.

我希望 cygwin 工作 - 已经做了很多努力来使它成为一个“标准”的 posix 环境。

The mingw toolchain uses msvcrt.dll for its runtime and thus will probably expose msvcrt's "thread" api: _beginthreadwhich is defined in <process.h>

mingw 工具链将 msvcrt.dll 用于其运行时,因此可能会暴露 msvcrt 的“线程”api:_beginthread,其定义在<process.h>