views:

876

answers:

5

Having no experience with threading in the past, which threading technique in C++ will be the easiest for a beginner? boost::thread or pthreads?

+4  A: 

boost::thread is a very nice and portable abstraction. I would certainly use it, but also learn the native thread api, like pthreads, so that you know how threading works on your platform.

St3fan
A: 

I'd say they're pretty close to equal in difficulty. The only big difference I see is that PThreads are pretty widely support (if you're concerned with cross platform porting). Another is that there have been quite a few good books on PThreads, though almost all the concepts will translate over to boost::thread, and many other threading libraries.

Chris S
pthreads are only available on *nix platforms by default (there is a pthreads wrapper available for Windows on Sourceforge). Boost is cross-platform, and supported on *nix and Windows alike.
jalf
Microsoft provides pthread headers and libraries with their Services for Unix package. For Win2K/XP/2003 it's a download:http://www.microsoft.com/downloads/details.aspx?FamilyID=896C9688-601B-44F1-81A4-02878FF11778For Vista/7/2008 it's a feature:http://technet.microsoft.com/en-us/library/cc771672.aspx
Chris S
+2  A: 

Boost.Thread uses the RAII concept for locking, which makes things more exception safe and helps to avoid bugs like forgetting to release a mutex.

Emile Cormier
+10  A: 

Go for boost::thread. It's closely related to the work on the upcoming C++ standard threads, and the interface is quite easy to use and idiomatic to C++ (RAII instead of manual resource management).

Malte Clasen
+4  A: 

I'll go in the opposite direction of everyone else - learn (or at least familiarize yourself with what is available in) pthreads.

Since boost is mainly just a wrapper around pthreads (on posix platforms) it helps to know what is going on underneath. In attempting to be generic, boost leaves the platform specific functionality unwrapped. In order to get to it you need to use the native_handle() calls. In order to use the native_handle() calls you need to know what the platform offers.

Think of it like sockets. There are dozens of socket classes and frameworks. But ultimately they wrap the underlying platform's socket API. Your understanding is always richer by knowing it - and knowing in what ways your class abstractions might have short comings.

Duck
Which particular functionality can't you access through Boost?
jalf
@jalf pthread attributes - priorities, etc. You can get at them via the native_handle() calls of Thread, Mutex, etc., but if you don't know a little about pthreads you may never know they are there to begin with.
Duck
Fair point. Just wish you'd mentioned them in the post itself. :)I'd also say that most apps shouldn't need to worry about thread priority (or process priority, for that matter). But you're right, if and when these details are needed, familiarity with the underlying API is essential.
jalf