Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lightweight portable C++ threading

Does anyone know about a lightweight portable C++ threading library, that can work on Windows, Linux and Mac OS X?

Specifically in my case, I do a simulator that after each time passes exports simulated data. I would like to run only one thread (simulate) that would once in a while start another thread (export). The only condition would be: if export thread started wait until it finishes, before starting a new one.

Thanks

like image 834
J.B. Avatar asked Nov 15 '10 15:11

J.B.


2 Answers

What about TinyThread++?

Need portable threads for your C++ app? Is C++0x unavailable for your target compiler(s)? Is Boost too large?

Then you need TinyThread++!

TinyThread++ implements a fairly compatible subset of the C++0x thread management classes.

like image 91
Milan Avatar answered Sep 30 '22 15:09

Milan


I use Boost.Thread and would recommend it to others.

It is portable to almost everything and easy to use. The "lightweight" thing is the only question, since I am not really sure what that means. Boost is lightweight in that there is almost no overhead to using it, since all the threading functionality is loose static wrapping for the underlying threading library (pthreads, Win32 API, Cell BE, etc). A "mutex" is really anything that implements that "Lockable" concept (see the documentation), which can be anything - even your own special whatever. In that sense, it is very lightweight and extensible.

However, Boost is a huge library and pulling just the parts of it you need can be extremely painful (this is a common complaint about Boost in general). Off the top of my head, using Boost.Thread means you have to have Boost.DateTime, Boost.System, Boost.ConceptCheck and Boost.Compiler (and probably more and whatever those rely on, etc). To their credit, it is very easy to build what you need if you have the whole library due to their automatic linking magic, but the need to have it all is definitely something to consider, especially if Windows is on the list of targets.

As an alternative to Boost, I would recommend OpenMP, assuming your compiler has support for it. The fact that it requires compiler support for some of the more advanced features might disqualify it on the "lightweight" thing, but it is pretty lightweight on usage (the first time you #pragma omp parallel for is pretty neat). It is not as feature-packed as Boost (I think only Qt can compete here), but using OpenMP gives you some really cool features that no other threading library can do. You'll have to use a somewhat modern compiler, but both GCC and MSVC have good support here. One caveat is that it is really a C library, which I see as a disadvantage if you're doing C++, but that could be a good thing for your requirements.

If you're looking for something significantly more lightweight (in both senses of the word), then I would recommend OpenThreads. It is nowhere near as extensible as Boost and find it less performant (not significantly, though), it is pretty well-designed and worth mentioning. It will hit all of your specified targets (Windows, OSX and Linux), so if it has the features you want, go for it.

Also, Wikipedia.

like image 40
Travis Gockel Avatar answered Sep 30 '22 13:09

Travis Gockel