Wednesday, February 8, 2012

POSIX threading API on Windows

When I was writing the Multicore book, I was struck by the commonality of the Windows [1][2] and POSIX threading APIs. The concepts were common, but the names (and parameters) of the function calls were different. It looked like a relatively trivial task to write a shim to translate between the two. However, I had a book to write, so it languished until one evening last week when I actually got around to doing it.

The result is these two files winpthread.h and winpthread.cpp. I don't claim that these are a high fidelity port, there's plenty missing, but they should be sufficient to get a code working. In fact, there's very little complexity in the code, the only caveat is that condition variables were not introduced in Windows until Vista, and these require the use of critical sections rather than mutexes. The difference between mutexes and critical sections is that critical sections are local to a single processes whereas mutexes can be shared between processes.

Let me know if there's bugs, any improvements, or if the code is useful.