C Programming – Current Best Practices for Multithreading

cmultithreading

I've been a Java (and PHP) developer for most of my life, but will need to use C for my next project. I remember some basic stuff we did back in school with fork, pthread, semaphores, etc. but I'm not sure if that's still the "way to go" here, or if there have been any recent developments/improvements etc.

Google's results seem to be fairly dated. Or does that just mean that this is still the current "state of the art"?

Or will I have to use a (system-independent) libary like glib if I want higher-level abstractions, like synchronized queues? What other libraries are there? (The programm will only have to run on Linux though)

Best Answer

Well, basically it's still the same. You still got pthreads (on Linux), forking is still used in some cases. Mutexes and semaphores are also there. Of course, there is CAS available but it's not like something really new.

What we have now is a few libraries for doing some multithreaded stuff a little differently. You can use libevent2 or libev for building event systems (though they are most useful in networking, for async reading and writing from sockets). And there is ZeroMQ which you may use to avoid direct communication of threads, it's actually pretty useful, just use their ipc transport.

As for data structures there are a few libs like glib or a lib from ZMQ guys.

Be warned though, C is still C and you must be as careful with it as ever.