15

Been doing mostly Java and smattering of .NET for last five years and haven't written any significant C or C++ during that time. So have been away from that scene for a while.

If I want to write a C or C++ program today that does some multi-threading and is source code portable across Windows, Mac OS X, and Linux/Unix - is PThread a good choice?

The C or C++ code won't be doing any GUI, so won't need to worry with any of that.

For the Windows platform, I don't want to bring a lot of Unix baggage, though, in terms of unix emulation runtime libraries. Would prefer a PThread API for Windows that is a thin-as-possible wrapper over existing Windows threading APIs.

ADDENDUM EDIT:

Am leaning toward going with boost:thread - I also want to be able to use C++ try/catch exception handling too. And even though my program will be rather minimal and not particularly OOPish, I like to encapsulate using class and namespace - as opposed to C disembodied functions.

5 Answers 5

15

Well, pthreads is the old posix standard for writing threaded programs. Its the lowest level threading routines, so its a good choice for cross-platform threading.

However, there are alternatives:

As the latter are all fully supported on all platforms, (pthreads requires a bit of compiler settings as its only part of Windows posix subsystem, unless you want to use Pthreads-w32), then perhaps the latter ones are a better choice. boost::threads are more like a threading library, the other 2 are high-level ways of achieving parallelism without needing to code 'threads', they allow you to write loops that run concurrently automatically (subject to common-sense conditions)

Boost::thread is not a C compatible library though.

edit: cross-platform abilities of the above:

Intel TBB is cross-platform (Windows*, Linux*, and Mac OS* X), supports 32-bit and 64-bit applications and works with Intel, Microsoft and GNU compilers.

OpenMP depends on the compiler you want to use, but GCC and/or Intel compilers have supported OpenMP Windows, Linux and MacOS.

6
  • "As the latter are all fully supported on all platforms" - If by all platforms you mean Windows and a couple of *nix platforms then this is true. Other than that it isn't. Feb 1, 2009 at 0:24
  • see my edit. The OP never asked for anything other than those 3 platforms.
    – gbjbaanb
    Feb 1, 2009 at 1:55
  • I accepted this response because it presented the widest range of information on possible ways to proceed.
    – RogerV
    Feb 1, 2009 at 3:57
  • Yeah, I think I'll go with boost:thread - I also want to be able to use C++ try/catch exception handling too. And even though my program will be minimal and not particularly OOPish, I like to encapsulate.
    – RogerV
    Feb 1, 2009 at 5:20
  • C++ used in its more-common "Better C" mode, a wise choice sir, and one that I hope you'll enjoy. Don't forget the rest of the STL and Boost though.
    – gbjbaanb
    Feb 1, 2009 at 14:21
11

If you need your code to be truly portable then it may be best to stay away from the various libraries that scatter the internet. At some point you'll find a platform they don't support and will then have to create your own branch.

This is also not a hard problem to solve and can be a good exercise for creating cross-platform code.

I'd suggest you create a class, e.g. CThread, that has separate .cpp implementations for each platform and a pure-virtual execute() function that is called after your thread is constructed/run.

That allows all of your thread-creation and sleep/shutdown/priority code to be implemented using the most appropriate API for the platform. You may also need a header (e.g. ThreadTypes.h) that contains defines/typedefs for each platform.

E.g.

// ThreadTypes.h
#if defined(PLATFORM_WIN) || defined(PLATFORM_XBOX)
  typedef DWORD ThreadID
#elif defined(PLATFORM_PS3)
  // etc etc
#endif

This is how I have written all my cross-platform threading code for platforms such as PC/PS2/PS3/360/Wii. It is also a good pattern to follow for things like mutex's and semaphores, which if you have threads you're certain to need at some point :)

4
  • I love it - anonymous downvotes from people who've likely never shipped an app on multiple platforms! FTW! Feb 1, 2009 at 2:05
  • Hey Andrew, I posted the original question. Was a bit surprised to see your's down-voted too. I've rolled thin C++ classes over Win32 threading - I'd just need to abstract that over POSIX Pthreads too and I'd be done. On the other hand, there is Pthreads-win32. Everything else has Pthreads already.
    – RogerV
    Feb 1, 2009 at 3:49
  • +1: Encapsulation is always your friend. It may seem like extra work, but that minimal extra time will eventually save your ass. Especially when somebody breaks standard and has unexpected error-values to test.
    – Mr.Ree
    Feb 1, 2009 at 7:24
  • +1, and I feel with you, Andrew. Rolling your own solution has the added benefit that you pay only for what you need, and that you get an understanding of the underlying principles that will help later when you need to fix bugs.
    – mghie
    Feb 1, 2009 at 11:27
5

Nope, pthreads aren't normally available on Windows. (There are a few attempts at implementing it, but it's not supported by the OS directly, at least.)

If you're writing C++, Boost is, as usual, the answer. Boost.Thread has a portable (and safer) threading library.

In C, the simplest solution is probably to wrap write a common wrapper for both pthreads and the Windows threading API.

0

I will bet on ZThread

Simple API, easier to use than PThreads and FREE

1
  • Any idea why I'm getting so many messages on my console when using Zthread library? Such as ThreadQueue created, User thread created , etc. It seems these are DEBUG messages from the library, how can I turn them off?
    – lqr
    Dec 1, 2014 at 9:55
0

Have a look at ting also: http://code.google.com/p/ting/

It is cross platform between Windows and Linux. No Mac OS support yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.