views:

703

answers:

8

I am looking for lightweight multi-threading framework for C++. I found POSIX Threads.
Please, share you practical experience with POSIX threads: before I start with it I want to know its pros and cons from real people, not from wiki.
If you practically compared it with anything (maybe, better), it would be interesting to know either.

UPD: cross platform features are really important for me, so I would appreciate this direction described.

UPD2: I already have an experience with one framework for QNX / Win32, but it is not lightweight and - oh, I forgot to mention, - it is commercial, not free, but I need a free one.

+12  A: 

I found Boost.Threads to be really nice, especially after the 1.35 rewrite. POSIX threads on Windows is not so trivial, and it's a C API, so I would definitely prefer Boost to it. It has all the stuff you need, is portable and requires little setup.

Anteru
Can you clarify a little bit, why is Boost better? I am not very Win-oriented. I would better prefer debugging support/simplicity/and so on.
avp
As Anteru wrote: pthreads is a C API while Boost is a C++ library.
rstevens
Boost isn't Windows-oriented either, but cross-platform. Unlike pthreads, it takes advantage of C++ features and is designed according to C++ idioms. Some of the things it gives you over pthreads are: type safety, more flexibility in passing parameters a new thread and simpler usage. Boost uses RAII extensively, so for example, to create a lock, you simply create a local object, and when it goes out of scope, the lock is released. Simpler than pthreads, where you explicitly have to call a function to release a lock. (and you're screwed if you forget, or an exception is thrown)
jalf
Short version: In C, use pthreads. In C++, use boost.
jalf
additionally if you use boost::thread it will be that much easier to port to std::thread when C++0x is fully adopted as std::thread is incredibly similar to boost::thread.
Rick
+2  A: 

The POSIX threading API is a C API, not C++.

What do you want to use it for? Personally, I find it to be a very clumsy and overly verbose API. But it is your best bet if you want to do cross-platform development on Unix/Linux-like operating systems. It is not natively supported on Windows.

Personally, I would not use a threading or any other OS dependent API directly in your code. Build another abstraction layer on top of it. For example, we built what we call an "OS layer"; a C++ framework for working with threads, semaphores, timers, mutexes, etc. Our code uses this exclusively. Underneath the hood, we have implementations for POSIX, Win32, INTEGRITY, and vxWorks. This lets our code work on a large variety of platforms.

If you don't want to build your own layer, you can look towards reusing many others like Boost, Qt, etc.

Brian Neal
There is a windows implementation - http://sourceware.org/pthreads-win32/
anon
Thanks I edited my response to say "not natively supported on Windows". You could also go the cygwin route on Windows if you wanted to use pthreads there.
Brian Neal
Don't write your own unless you really want to be in the library business, or unless what you need is so simple that it's trivial. Threading on different OS's (Windows vs. Linux, for instance) works very differently, and if you don't get that right in your abstraction up front, you end up with problems.
Michael Kohne
What do you mean for "clumpsy"? Is it too low-level or something else?
avp
@Michael Kohne: It's not that big deal to write simple-to-use wrapper to hide different implementations. Threading is working very equal in the 95% of functionality that is needed for the daily work.
rstevens
@Michael Kohne: It is indeed fairly easy to write your own C++ wrapper around various OS facilities. Plus we cannot use boost in our environment (or other 3rd party utilities).
Brian Neal
@avp - I said "clumsy". It is a C API, not C++, and a very verbose one at that. You have to call a lot of functions to configure things like stack size, priority, etc. It isn't very difficult to figure out though.
Brian Neal
+1  A: 

I used POSIX a while ago for a program I wrote. It worked fine on Linux and Solaris and it's not terribly complicated to implement. My brother on the other hand is a Windows programmer and preferred boost to Posix. I guess it depends on your target. I found boost to be a bit on the bloated side and had heard bad things about it. My brother thinks it's the greatest thing since sliced bread. I suppose it's a ford vs chevy thing. Everyone will have an opinion.

ProZach
+2  A: 

If you don't like Boost's thread API, then you might want to look at POCO's.

Michael Kohne
A: 

I've found it to be pretty similar to the win32 thread API, the only (real) difference you need to be aware of is that win32 mutexes don't block when used on the same thread while posix do. Apart from that, it's a pretty straight forward API.

Jasper Bekkers
rstevens
+1  A: 

As you are mentioning QNX have a look at ACE. It is a vast framework that is available for many platforms (including QNX). Others have already mentioned Boost.

You are well advised to use one of these libraries instead of the low level, non portable and error prone C APIs.

lothar
+1  A: 

Another C thread API is gthreads from glibc. There is a 1-to-1 mapping between some gthread and pthread calls such as pthread_create, but gthreads have 2 big features that I have found very useful: thread pools and asynchronous queues for sending messages between threads. The thread pools are very powerful, allowing things like dynamic resizing of the pool. See http://library.gnome.org/devel/glib/2.20/glib-Threads.html

bobmcn
+1  A: 

Boost threads library is probably your best bet if you work in C++. I had very positive experience with it both on Unix and win32. Avoid ACE - bad design, wrong approach. Also take a look at Intel TBB, though I haven't used it in practice.

Nikolai N Fetissov