Is there any cross-platform threading library in C++?

前端 未结 12 986
小鲜肉
小鲜肉 2020-12-14 01:36

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

相关标签:
12条回答
  • 2020-12-14 02:01

    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.

    0 讨论(0)
  • 2020-12-14 02:03

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

    What platforms will you support?

    0 讨论(0)
  • 2020-12-14 02:06

    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.

    0 讨论(0)
  • 2020-12-14 02:09

    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.

    0 讨论(0)
  • 2020-12-14 02:13

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

    0 讨论(0)
  • 2020-12-14 02:14

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

    0 讨论(0)
提交回复
热议问题