26

I'm looking for some easy to use cross-platform threading library written in C++.

What's your opinion on boost::thread or Pthreads? Does Pthreads run only on POSIX compliant systems?

What about the threading support in the Qt library?

Sarang
  • 1,867
  • 1
  • 16
  • 31
NumberFour
  • 3,551
  • 8
  • 48
  • 72

12 Answers12

33

Boost.Thread is the draft for the coming standard threading library of the C++ language. Knowing that, I prefer to use it as it provide some strong guarantees (because it becomes standard).

Update: Now that we have the standard threading libraries, some more precisions. Some boost constructs, like boost::shared_mutex, have not been standardised (but might be later). However the standard library exploit the move semantic better. Good to know before choosing a library. Also, using C++11 threading library requires a compiler that provides it. It's not the case for all compilers today.

Update: Now [Nov2012] most of the Standard compilers provide C++11 threading library. VS2012, GCC4.8 and Clang3.1 have support for threads and synchronization primitives and atomic operations. For complete implementation you can also use just thread by Anthony Williams. It is C++11 compliant library supported on Windows/Mac and Linux.

Links for status of C++11 features with various compilers:

Sarang
  • 1,867
  • 1
  • 16
  • 31
Klaim
  • 67,274
  • 36
  • 133
  • 188
  • I know this answer is old, but I'd like to add that gcc 4.6 won't compile Boost.Thread in C++11 mode. This might be an issue in any of them, but most likely gcc, since C++11 support is under development at the moment. – Tamás Szelei Jan 31 '12 at 09:58
  • I'll add that you still have to have a compiler providing the standard threading library to use it, or otherwise rely on boost. – Klaim Jan 31 '12 at 10:00
9

There is a threading library coming with C++11. It's built upon the boost threading library. Unfortunately, I seem to remember that there are non-trivial differences between Boost.Threads and what C++11 comes with. Still, if you plan to switch to the C++ standard threading library, I believe Boost.Threads is the closest you can get to now.

I suppose that, under the hood, these libraries will use Pthreads on POSIX systems and whatever native threading support is available elsewhere.

Disclaimer: I haven't worked with either of the two.

sbi
  • 219,715
  • 46
  • 258
  • 445
4

Pthreads are running only on POSIX systems. QThread from Qt is a way to go. It is available on platforms: Linux, Mac OS X, Windows, Embedded Linux, Windows CE, Symbian, Maemo.

zoli2k
  • 3,388
  • 4
  • 26
  • 36
  • 2
    What do you mean saying "All platforms" – osgx Apr 01 '10 at 15:25
  • 3
    Yeah, I have this chip here in my toaster, is Qt available for it? `:)` – sbi Apr 01 '10 at 15:28
  • Officially supported, win/mac/unix+x/embedded unix/wince/symbian/meamo - Probably more than boost. – Martin Beckett Apr 01 '10 at 15:29
  • sbi> yes if your toast provides enough memory! – zoli2k Apr 01 '10 at 15:35
  • @shakov: With the bread they sell here, the toast's memory is _very_ limited, but I suppose I could scratch a few digits into its surface. But then, it's lifetime really is too limited to go to the trouble. `:)` – sbi Apr 02 '10 at 16:09
4

Also have a look at OpenMP, it's a set of (somewhat standard) pragmas specifications that is supported by most major compilers. The good of OpenMP is that it's simple and that your code can be easily compiled in both single and multi-threaded versions.

Just a simple example:

std::vector<double> a, b;
...
double sum = 0.0;
...
#pragma omp parallel for reduction(+:sum)
  for (i=0; i < n; i++)
    sum = sum + (a[i] * b[i]);

It's obviously possible to do also more complex things.

Community
  • 1
  • 1
baol
  • 4,362
  • 34
  • 44
  • Is OpenMP supported on Windows too? – NumberFour Apr 01 '10 at 15:40
  • Yes, OpenMP is supported on Windows, by Visual Studio 2005 and later, and of course Intel compilers. – Chris O Apr 01 '10 at 15:46
  • And it should not introduce any dependency. – baol Apr 01 '10 at 19:07
  • But the fact is that OpenMP is designed rather for parallel task execution than just regular threading which deals with synchronization, mutexes...etc. Right? I mean OpenMP isn't just suitable for creating a multithreaded server for example. – NumberFour Apr 04 '10 at 12:25
  • OpenMP definitely has synchronization primitives, but yes, I'd not use it to write a web server. – baol Apr 04 '10 at 14:23
3

I am surprised that nobody mentioned the Intel TBB library (linked to an another answer of mine). Also, a task-based implementation should be preferred over a thread-based.

Community
  • 1
  • 1
amit kumar
  • 20,438
  • 23
  • 90
  • 126
  • Yes, also the future-async model in C++11 is quite easy to go with. For Windows only PPL is very similar to TBB and is well supported by MS – Sarang Nov 16 '12 at 23:20
2

Qt has pretty good thread support. If you just need to create a thread and run some code in it, QThread is all you need. There are many other high-level classes that can help you with thread pools or even abstract the concurrent execution (the QtConcurrent framework).

Lukáš Lalinský
  • 40,587
  • 6
  • 104
  • 126
  • 1
    Qt indeed has a good threading library; it works very well for Qt applications since you can connect signals across threads. If you're using Qt elements, I would stick with Qt threads. – Will Apr 06 '10 at 17:47
2

List the concerning platforms. If you're only using say, Linux/Mac/Windows, then boost::thread will likely do you fine until C++0x (harhar) provides std::thread.

Puppy
  • 144,682
  • 38
  • 256
  • 465
2

I have used pthreads for code that work on multiple platforms. To get around the Windows lack of pthreads, I have used the following open source library with great success: POSIX Threads for Windows

Starkey
  • 9,673
  • 6
  • 31
  • 51
  • pthreads-w32 is old school. mingw-w64 supplies an improved version called winpthreads. –  May 06 '15 at 09:31
1

wxWidgets has thread classes, and as wxWidgets is platform independent, it might just be the best thing for u.

the100rabh
  • 4,077
  • 4
  • 32
  • 40
1

Boost.Threads is built on top of PThreads on UNIX systems and Win32 Threads on Windows.

The boost library is syntactically simple and all of the hairy business of properly interfacing C++ code with C libraries is taken care of behind the scenes. If you're not very comfortable with C++, however, PThreads might seem more straight-forward with its simple C API.

Qt Threads is also a good library, but because I use several other boost libraries, I'll compile and link against Boost no matter what. I might not always link against Qt. And, well, I just don't want to remember how to use two different libraries.

Dr. Watson
  • 3,752
  • 4
  • 32
  • 43
0

SDL is simple, cross-platform and has threading support.

SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87
0

Pthread is part of Posix, but not every posix systems will have threads. pthreads is most portable.

What platforms will you support?

osgx
  • 90,338
  • 53
  • 357
  • 513