views:

2850

answers:

3

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.

+4  A: 

This isn't so much C's pthread.h as Linux's pthread.h - it's a header for the Unix/Linux API for threads. So I'd say no.

The windows API is exposed via #include <windows.h> and is documented at http://msdn.microsoft.com and works differently to Linux's threading.

Edit: Although a quick google reveals: http://sourceware.org/pthreads-win32/ and I imagine cygwin would compile an app #including pthreads.h.

Ninefingers
I did find pthreads-w32, but how do I get that to work with mingw(preferred) or cygwin?
naspinski
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
Ninefingers
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
Ninefingers
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
@Christoph: how about pthread_key_create and friends?
Steve Jessop
+7  A: 

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
Ninefingers
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
Sorry, I forgot to mention I didn't want to use this library for just the reason you state, thank you.
naspinski
A: 

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