2

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.

8 Answers 8

12

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.

4
  • 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
    Apr 27, 2009 at 17:55
  • As Anteru wrote: pthreads is a C API while Boost is a C++ library.
    – mmmmmmmm
    Apr 27, 2009 at 18:14
  • 2
    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
    Apr 27, 2009 at 22:37
  • 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
    May 9, 2009 at 0:46
3

Another C thread API is GThreads from GLib. 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

2

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.

6
  • 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
    Apr 27, 2009 at 17:41
  • 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. Apr 27, 2009 at 17:57
  • What do you mean for "clumpsy"? Is it too low-level or something else?
    – avp
    Apr 27, 2009 at 18:06
  • @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.
    – mmmmmmmm
    Apr 27, 2009 at 18:18
  • @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
    Apr 27, 2009 at 20:50
2

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.

2

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

1

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.

1

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.

0

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.

1
  • 1
    The mutex thing on posix can be avoided by flagging them as recursive (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE))
    – mmmmmmmm
    Apr 27, 2009 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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