PThreads Win32 In Visual Studio 2012
Feb 6, 2014
2 minute read

One popular option for portable multi-threading is using the POSIX threads (pthreads) library. On Unix-like platforms like Linux this can be installed easily through a package manager. On Mac OS X you can use something like Homebrew to install it.

On Windows the situation is a little less clear. Most people appear to use the Win32 port found here. When you get the latest package, you’ll find that the project files are for a very old version of Visual Studio. The upgrade option gave me errors. Using git to grab the latest which had VS2012 solutions also did not compile correctly.

The solution was to actually RTFM found at ftp://sourceware.org/pub/pthreads-win32/dll-latest/README and use the command line tools to build. Open the Developer Command Prompt from the start menu, cd to the pthreads.2 source directory found in a package (like ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-9-1-release.zip), and use one of the following commands:

nmake clean VCE (to build the MSVC dll with C++ exception handling)
nmake clean VSE (to build the MSVC dll with structured exception handling)
nmake clean VC (to build the MSVC dll with C cleanup code)
nmake clean VCE-inlined (to build the MSVC inlined dll with C++ exception handling)
nmake clean VSE-inlined (to build the MSVC inlined dll with structured exception handling)
nmake clean VC-inlined (to build the MSVC inlined dll with C cleanup code)
nmake clean VC-static (to build the MSVC static lib with C cleanup code)
nmake clean VCE-debug (to build the debug MSVC dll with C++ exception handling)
nmake clean VSE-debug (to build the debug MSVC dll with structured exception handling)
nmake clean VC-debug (to build the debug MSVC dll with C cleanup code)
nmake clean VCE-inlined-debug (to build the debug MSVC inlined dll with C++ exception handling)
nmake clean VSE-inlined-debug (to build the debug MSVC inlined dll with structured exception handling)
nmake clean VC-inlined-debug (to build the debug MSVC inlined dll with C cleanup code)
nmake clean VC-static-debug (to build the debug MSVC static lib with C cleanup code)

So a command like this will build the release version that doesn’t use exceptions:

nmake clean VC

After building the release version you can use VC-debug to build the debug copy of the DLL. Copy the .lib and the .dll files to a directory in your project, set the library and include paths and you’re good to go!