views:

361

answers:

6

I'm looking for a development job and see that many listings specify that the developers must be versed in multithreading. This appears both for Java job listings, and for C++ listings that involve "system programming" on UNIX.

In the past few years I have been working with Java and using its various synchronization mechanisms.

In the late 90s I did a lot of C++ work, though very little threads. In college, however, we used threads on Solaris.

My question is whether there are significant differences in the issues that developers in C/C++ face compared to developers in Java, and whether any of the techniques to address them are fundamentally different. Java obviously includes some nicer mechanisms and synchronized versions of collections, etc.

If I want to refresh or relearn threading on UNIX, what's the best approach? Which library should I look at? etc. Is there some great current tutorial on threads in c++?

+3  A: 

Garbage collection makes programming threads that do not leak memory easier, and there are fancy things you can do to address the timing of the collections.

Deterministic destructors make programming threads that do not spawn zombies easier, see ACM paper here

Thomas L Holaday
This answer needs clarification.
Cannonade
Okay, I added the adjective deterministic to destructors and linked to an ACM paper.
Thomas L Holaday
+3  A: 

Look at pthreads and boost (the pthreads one was a random lijnk, but it looks ok as a starting point).

At a high level the issues for Java/C/C++/ are the same. The specifics about how you solve the problem (functions to call, classes to create, etc...) vary language to language.

TofuBeer
+7  A: 

The fundamental challenges of threading (e.g. synchronization, race conditions, inter-thread communication, resource cleanup), but Java makes thread much more manageable with garbage collection, exceptions, advanced synchronization objects, advanced debugging support with reflection.

With C++, you are much more likely to have memory corruption and "impossible" race conditions. And you will need to write a lot more low-level thread primitives or rely on libraries (like boost) that are not part of the standardized language.

cpeterso
The next C++ standard, due shortly, will address threading an synchronization quite close to boost library implementation. If you want to go for C++ threading, consider boost as a good start point.
David Rodríguez - dribeas
The terms "boost" and "C++ standard library" are becomming more and more merged. It is my understanding that boost was founded during a C++ standard meeting in order to encourage the development of libraries for C++.
Richard Corden
A: 

Regardless of the programming language being uses, the idiosyncrasy of thread are common. For instance even across OS the POSIX threads & WIN32 threads have same set of logical idiosyncrasies, though the API calls & native implementation WRT underlying hardware/kernel might change, but to system programmers the logical thinking about threads & how to make them work as expected & in achieving this is the most hardest part. This is even true when coming to programming languages. If you really understand the concept of threading & thread synchronization you are good to go & use them in any programming languages you like. Since these programming languages provide syntactic sugars on top of the native thread/thread synchronization implementation.

placidhacker
+1  A: 

It depends on what level you choose to work at. Intel TBB and OpenMP handle a lot of common cases from a pretty high level. Posix threads, Windows APIs, and portable libraries like Boost threads bring you closer to the same level as primitives in Java.

C++0x threading (especially with acquire and release memory barriers) allow you to go to an even lower level for more control and complexity than Java offers (marking a variable volatile in Java gives it both an acquire and a release memory barrier, but in Java you can't ask for just the acquire or just the release barrier; where in C++0x you can).

Please note that C++0x's threading model is intentionally low level with the hope that people will build things like TBB on top of it and the next time the standards committee meets they'll be able to figure out which of those higher level libraries and toolkits work well enough to learn from.

Max Lybbert
+3  A: 

C++ is actually aeasier to write complex threaded code in than Java because it has a feature Java lacks - RAII or "resource acquisition is initialisation". This idiom is used for all all resource control in well written C++ code, but is particularly appropriate in multi-threaded code where automatic management of synchronisation is a must.

anon
I was going to post about this. RAII is a great tool for control of synchronization primitives.
David Rodríguez - dribeas
C++ RAII syntax is slightly neater but less obvious than Java synchronized. I don't think that impacts whether it is easier in either language. However, say, GC does make certain things simpler.
Tom Hawtin - tackline
C++ RAII syntax means you don't have to get your finally clauses right every time.
Max Lybbert