开发者

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.

Than开发者_开发百科ks


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.


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.


Boot.Thread is what you are looking for. To quote its description in Boost's doc pages

Portable C++ multi-threading.


Boost.Thread would work here.

You can use join to wait for an existing thread to finish.

There are other code samples in the docs but starting a thread looks like this:

struct callable
{
    void operator()();
};

boost::thread copies_are_safe()
{
    callable x;
    return boost::thread(x);
} // x is destroyed, but the newly-created thread has a copy, so this is OK

boost::thread oops()
{
    callable x;
    return boost::thread(boost::ref(x));
} // x is destroyed, but the newly-created thread still has a reference
  // this leads to undefined behaviour


I've mostly heard about Boost.Thread which can be pretty heavy, Poco.Thread which is supposed to be lightweight and Intel's TBB which I don't know how it works.

I did a little experimentation with C++0x but I've been told it's not mature enough yet for complex implementations.


I use the c++11 Standar for threads to create a class multithreading and it works for Linux, Windows and OS X, may it be could be use full for you:

https://github.com/jorgemedra/C-11-Threads/blob/master/README.md

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜