68

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?

I am using Vista with the latest MinGW.

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.

naspinski
  • 34,020
  • 36
  • 111
  • 167

5 Answers5

60

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

Community
  • 1
  • 1
  • 3
    I did find pthreads-w32, but how do I get that to work with mingw(preferred) or cygwin? – naspinski Jan 27 '10 at 23:13
  • 1
    There's a pre-compiled .lib file which you could build into your app like this: gcc -o app.exe file.o file2.o pthreads-win32.a (.a is deliberate, that's the convention they've used) having #included it (the header), or you could also link to the DLL which is also pre-built. Readme available at: ftp://sourceware.org/pub/pthreads-win32/dll-latest/README –  Jan 27 '10 at 23:21
  • Dlls are used like at the bottom of the page: http://www.adp-gmbh.ch/win/misc/mingw/dll.html i.e. gcc {c files} {dlls} -o app.name –  Jan 27 '10 at 23:25
  • 1
    don't use cygwin; pthreads works fine with MinGW; I never got thread-local storage to work, though: the compiler supports `__thread`, but someone else (linker? loader? library?) doesn't play nice... – Christoph Jan 27 '10 at 23:29
  • 2
    Oh, if you insist on using another API to code threads on Windows (even though Windows already has a threading API) be sure you read the list of [__known bugs__](http://sourceware.org/pthreads-win32/bugs.html). – bobobobo Aug 21 '13 at 10:20
  • (2018) MinGW now ships with its own threading library referred to as winpthreads (see https://github.com/mirror/mingw-w64/tree/master/mingw-w64-libraries/winpthreads ) which it uses by default. The original pthreads-w32 project renamed itself to pthreads4w and is now hosted on https://sourceforge.net/projects/pthreads4w/ . – Anon Mar 10 '18 at 12:49
18

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

Randolpho
  • 55,384
  • 17
  • 145
  • 179
  • 3
    Aaahh forgot about that - ought to note that it compiles apps against a different subsystem though; you can't as far as I'm aware mix Win32 and SFU APIs. So no windows GUI & pthreads if you use SFU. –  Jan 27 '10 at 23:01
  • 1
    Excellent point. @naspinski: Remember... porting is difficult and fraught with peril. If you're writing new code, use win32. If porting, try to use SFU, but beware interactions; things could get ugly. – Randolpho Jan 27 '10 at 23:04
  • Sorry, I forgot to mention I didn't want to use this library for just the reason you state, thank you. – naspinski Jan 27 '10 at 23:14
  • Does this library exist for Vista? OP mentioned he is using Vista... I'm also looking for such a library :-) – Mayank May 23 '11 at 06:45
  • @Mayank: it's a C library that utilizes Win32, and I'm reasonably certain none of the Win32 library calls have changed since the library was released to the Vista release. Vista should be backwards compatible with the library. Can't hurt to try. – Randolpho May 23 '11 at 15:19
  • @Randolpho Despite the fact that it explicitly says on the download page that it is not? – Leif Andersen Aug 04 '11 at 22:53
  • 6
    FYI, the Subsystem for UNIX-Based Applications is being phased out. It is depreciated in Windows 8 and will be removed thereafter. – bwDraco Sep 12 '12 at 16:43
13

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.

bobobobo
  • 64,917
  • 62
  • 258
  • 363
4

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.

Keyboard Penman
  • 162
  • 1
  • 11
3

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>

Chris Becke
  • 34,244
  • 12
  • 79
  • 148