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

nas*_*ski 55 c windows multithreading pthreads

如果我尝试编译程序

#include <pthread.h>
Run Code Online (Sandbox Code Playgroud)

在其中,我收到错误:

pthread.h: No such file or directory
Run Code Online (Sandbox Code Playgroud)

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

我正在使用Vista与最新的MinGW.

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

小智 49

pthread.h是用于线程的Unix/Linux(POSIX)API的标头.像Cygwin这样的POSIX层可能会编译一个应用程序#include <pthreads.h>.

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

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

  • 我确实找到了pthreads-w32,但是如何让它与mingw(首选)或cygwin一起工作? (3认同)
  • 哦,如果你坚持使用另一个API来编写Windows上的线程(即使Windows已经有一个线程API),请务必阅读[__known bugs __]列表(http://sourceware.org/pthreads-win32/bugs. HTML). (2认同)

Ran*_*pho 17

正如@Ninefingers所提到的,pthreads只是unix-only.只有Posix,真的.

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

适用于UNIX 3.5的Microsoft Windows服务

图书馆下载

  • 仅供参考,基于UNIX的应用程序子系统正在逐步淘汰.它在Windows 8中折旧,之后将被删除. (6认同)
  • Aaahh忘了这一点 - 应该注意它虽然可以针对不同的子系统编译应用程序; 你不能知道混合Win32和SFU API.所以如果你使用SFU,没有Windows GUI和pthreads. (3认同)

bob*_*obo 11

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

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

线程在不同的系统上几乎一样,你有ThreadPools互斥体.使用过两种pthreads和Windows线程之后,我可以说Windows线程提供的功能远远超过了pthread它.

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