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

后端 未结 5 815
囚心锁ツ
囚心锁ツ 2020-11-27 17:14

If I try to compile a program with

#include 

in it, I get the error:

pthread.h: No such file or directory
         


        
相关标签:
5条回答
  • 2020-11-27 17:15

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

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

    Microsoft Windows Services for UNIX Version 3.5

    Library Download

    0 讨论(0)
  • 2020-11-27 17:23

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

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

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

    0 讨论(0)
  • 2020-11-27 17:26

    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.

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

    You'll need to link to the libwinpthread.a library 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.

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

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

    Hope this helps.

    0 讨论(0)
  • 2020-11-27 17:31

    pthread.h isn't on Windows. But Windows has extensive threading functionality, beginning with 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.

    Threads pretty much work the same on different systems, you have ThreadPools and mutexes. Having worked with both pthreads and Windows threads, I can say the Windows threading offers quite a bit more functionality than pthread does.

    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.

    0 讨论(0)
  • 2020-11-27 17:41

    pthread.h is 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>.

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

    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).

    0 讨论(0)
提交回复
热议问题