views:

1882

answers:

6

Hi all, I am porting my application to windows from Linux. I am fairly new to the fine-art of porting application across platforms. As far as I know, Windows does not natively support POSIX threads implementation. Is this true? I have heard about some implementation of pthreads for windows (a wrapper or something), would it be better to use them or use CreateMutex and other APIs provided by windows???? Someone pls. enlighten me with the PROs and CONs of both worlds. Some miscellaneous tips for porting would go nicely along with the answer.

Thanks in advance.

+2  A: 

One thing you need to keep in mind is what is the future of this code. Do you plan on developing (and releasing) on both platforms in the future? Or is this a one way port?

The best thing to do when porting a project is to keep the actual changes to the code as minimal as possible. In your case, this would mean going with a pthread solution. That being said, if you are planning this to be a one way port, going native never hurts. :)

I would take some time to fully examine both stratigies and then implement the one you feel most comfortable with.

Craig
+7  A: 

It's all going to be the same stuff (pthreads is just going to call EnterCriticalSection etc), so if you've got a pthreads wrapper, you should probably use it so that you don't have to change as much code

Paul Betts
A: 

Why not have the best of both worlds and use a library that wraps both pthreads and Window's API and uses the appropriate one under the covers? Your code stays the same on both platforms.

There are no shortage of such libs in C++ so I can't imagine there aren't C versions about.

Duck
The APR (Apache Portable Runtime) is a fantastic cross-platform C library that does this. Highly recommended as it is well tested and, um, rather popular.
Clay
A: 

On Windows C/C++ applications that use the CRT need to call beginthread/beginthreadex to properly initialize the CRT in the new thread.

Remus Rusanu
+1  A: 

The first thing I'd do is to port to Boost Thread under Linux than to Windows.

Shing Yip
So how is Boost Threads better than POSIX?
puffadder
Going forward, Boost Threads, which the next standard library thread base on, will be the recommended way for doing threading in cross platform C++. POSIX thread API and raw Win32 threading API are not exactly easily interchangeable. Condition variable, for example, is available in POSIX but not on all Win32 platforms. If you're going to rely on a library, a POSIX/Win32 wrapper for example, you run the risk of not knowing the quality of the library, and you may not be able to get a lot of help. Boost on the other hand, you're sure to find lots of help when you run into problems.
Shing Yip
Thanks. That was a help. Is it available for C?
puffadder
No, Boost is C++ only.
Shing Yip
+2  A: 

this works well: http://sourceware.org/pthreads-win32/

It is a port of the pthreads library for Windows.

I'll throw in my 2 cents that we've had very good luck with this library as well. A fairly complex application with lots of semaphores, queues, and theads was ported from VxWorks to POSIX and finally onto Windows with this.
Chris Arguin