Thread programming in Windows

IEEE POSIX 1003.1 Threads programming is supported directly in Linux GCC compiler. But for windows, you need to install pthread API.(More about it here) The following procedure tells how to go about it.
I am using MinGW compiler for compiling my C programs, as it is the GCC for windows.

Compiling Pthreads programs on Windows platform:
1. Install MinGW
2. From ftp://sourceware.org/pub/pthreads-win32,
Download latest version of “pthreads-w32-v-v-v-release.exe” where v-v-v is the version number.
(i did pthreads-w32-2-8-0-release.exe)
3. Run the exe(Double-click).
4. It’ll ask for extract. Extract the files to the desired location.
5. Done
6. At the location specified above, you will see following 3 folders:

QueueUserAPCEx
Pre-built.2
pthreads.2

7. Open command prompt(Start>Run>cmd)
8. cd to the above pthreads.2 folder
9. c:\pthreads.2>make
10. c:\pthreads.2>make clean GC
11. c:\pthreads.2>cd tests
12. c:\pthreads.2\tests>make
13. c:\pthreads.2\tests>make clean GC
14. This is all. Now go to the folder where u have your pthread program to be compiled, say myapp.c.
15. Now copy the following files to this folder

pthread.h |
sched.h |> from “Pre-built.2\include”
semaphore.h |
pthreadGC2.dll from “Pre-built.2\lib”
libpthreadGC2.a from “Pre-built.2\lib”

16. Now in the command prompt cd to your folder
17. To compile myapp.c type following code:

> gcc -o myapp.exe myapp.c -I. -L. -lpthreadGC2
> myapp

That’s it.
From now on u need to do the Steps 15-17.

The following reference tells how to compile directly:
Building applications with GNU compilers
—————————————-

If you’re using pthreadGC2.dll:

With the three header files, pthreadGC2.dll and libpthreadGC2.a in the
same directory as your application myapp.c, you could compile, link
and run myapp.c under Mingw32 as follows:

gcc -o myapp.exe myapp.c -I. -L. -lpthreadGC2
myapp

Or put pthreadGC2.dll in an appropriate directory in your PATH,
put libpthreadGC2.a in your system lib directory, and
put the three header files in your system include directory,
then use:

gcc -o myapp.exe myapp.c -lpthreadGC2
myapp

If you’re using pthreadGCE.dll:

With the three header files, pthreadGCE.dll, gcc.dll and libpthreadGCE.a
in the same directory as your application myapp.c, you could compile,
link and run myapp.c under Mingw32 as follows:

gcc -x c++ -o myapp.exe myapp.c -I. -L. -lpthreadGCE
myapp

Or put pthreadGCE.dll and gcc.dll in an appropriate directory in
your PATH, put libpthreadGCE.a in your system lib directory, and
put the three header files in your system include directory,
then use:

gcc -x c++ -o myapp.exe myapp.c -lpthreadGCE
myapp

Enjoy!

Advertisements