【问题标题】:Can I get Unix's pthread.h to compile in Windows?我可以让 Unix 的 pthread.h 在 Windows 中编译吗?
【发布时间】:2011-01-10 04:36:32
【问题描述】:

如果我尝试用

编译一个程序
#include <pthread.h>

在里面,我得到了错误:

pthread.h: No such file or directory

是否可以在 Windows 环境中编译?

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

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

【问题讨论】:

    标签: c windows multithreading pthreads


    【解决方案1】:

    pthread.h 是用于线程的 Unix/Linux (POSIX) API 的标头。 Cygwin 等 POSIX 层可能会使用 #include &lt;pthreads.h&gt; 编译应用程序。

    原生 Windows 线程 API 通过 #include &lt;windows.h&gt; 公开,它的工作方式与 Linux 的线程略有不同。

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

    【讨论】:

    • 我确实找到了 pthreads-w32,但是如何让它与 mingw(preferred) 或 cygwin 一起使用?
    • 有一个预编译的 .lib 文件,您可以像这样构建到您的应用程序中: gcc -o app.exe file.o file2.o pthreads-win32.a (.a 是故意的,那是他们使用的约定)具有#included它(标题),或者您也可以链接到同样预构建的DLL。自述文件位于:sourceware.org/pub/pthreads-win32/dll-latest/README
    • Dlls 在页面底部使用:adp-gmbh.ch/win/misc/mingw/dll.html 即 gcc {c files} {dlls} -o app.name
    • 不要使用cygwin; pthreads 与 MinGW 配合良好;不过,我从来没有让线程本地存储工作:编译器支持__thread,但其他人(链接器?加载器?库?)表现不佳......
    • 哦,如果您坚持在 Windows 上使用另一个 API 来编写线程代码(即使 Windows 已经有线程 API),请务必阅读known bugs 的列表。
    【解决方案2】:

    正如@Ninefingers 所提到的,pthread 仅适用于 unix。仅 Posix,真的。

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

    Microsoft Windows Services for UNIX Version 3.5

    Library Download

    【讨论】:

    • Aaahh 忘记了 - 应该注意它编译应用程序针对不同的子系统;据我所知,你不能混合 Win32 和 SFU API。所以如果你使用 SFU,就没有 windows GUI 和 pthreads。
    • 好点。 @naspinski:记住……移植是困难的,而且充满了危险。如果您正在编写新代码,请使用 win32。如果移植,尽量使用SFU,但要注意交互;事情可能会变得丑陋。
    • 对不起,我忘了说我不想使用这个库只是因为你说的原因,谢谢。
    • 这个库是否存在于 Vista 中? OP提到他正在使用Vista ...我也在寻找这样的图书馆:-)
    • 仅供参考,基于 UNIX 的应用程序的子系统正在逐步淘汰。它在 Windows 8 中已贬值,此后将被删除。
    【解决方案3】:

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

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

    线程在不同系统上的工作方式几乎相同,您有ThreadPools 和mutexes。在使用过pthreads 和Windows 线程之后,我可以说Windows 线程提供的功能比pthread 多很多。

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

    【讨论】:

      【解决方案4】:

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

      它包含pthread.h 标头、libwinpthread.a、libwinpthread.dll.a 32 位和 64 位静态库以及所需的 .dlls libwinpthread-1.dll 和 libwinpthread_64-1.dll(截至 01-06 -2016)。

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

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

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

      希望这会有所帮助。

      【讨论】:

        【解决方案5】:

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

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

        mingw 工具链在其运行时使用 msvcrt.dll,因此可能会暴露 msvcrt 的“线程”api:_beginthread,它在 &lt;process.h&gt; 中定义

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-03-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-06
          • 2010-12-15
          相关资源
          最近更新 更多