3

I'm writing a library in C++ language that uses multithreading. Since I'm developing it in windows, I'm using the method and the data structures in "Windows.h" (like CreateThread, HANDLE WINAPI, etc).

Now I would like to make this code executable both in Windows and in Linux; can you suggest to me some threading implementations to do that?

The main requirement of this application is execution speed, so I need some implementation that is fast.

Thank in advance.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Aslan986
  • 9,984
  • 11
  • 44
  • 75
  • 6
    The C++11 standard library supports multi-threading. – cmannett85 Apr 20 '12 at 14:07
  • @cbamber85 - yes that's true but be wary of blanket advice based on that. Actual compiler support for C++11 (a non-trivial task) is ongoing and variable. – Steve Townsend Apr 20 '12 at 14:41
  • @SteveTownsend You're right, I assumed that gcc concurrency support would be at a similar level to the C++11 language support - turns out it's nowhere near. – cmannett85 Apr 20 '12 at 16:16

4 Answers4

12

Your best bet by far is to use the new std::threads library, this is portable and standardised and written in a modern style.

 std::thread my_thread(my_func, my_param);

http://en.cppreference.com/w/cpp/thread

If you don't have access to C++11 (VC10 and >gcc 4.4 should be fine), then the std::threads are more or less a developement of the great boost::threads, the boost library is cross platform and portable (at least it will support major OS include Win32 & linux).

http://www.boost.org/doc/libs/1_49_0/doc/html/thread.html

Finally if you are looking for parallelising algorithms it might be worth your while checking out intel's TBB, that is a modern C++ thread library that provides parallel constructs similar to the std:: algorithms

tbb::for_each(my_contaier.begin(), my_container.end(), my_func);

http://threadingbuildingblocks.org/

111111
  • 15,686
  • 6
  • 47
  • 62
  • I've has some little issues with earlier boost thread libs in Windows. The threads ran OK, but closing the app generated an AV. It's probably all fixed in the later releases - I think I have 1.35, which is plenty old :( – Martin James Apr 20 '12 at 14:15
  • According to this thread there are known problems in VC10 for `std::thread`. Latest Boost might be a better bet in the meantime. http://stackoverflow.com/questions/9421235/stdthread-class-in-vc-11-causes-random-crashes-any-workarounds – Steve Townsend Apr 20 '12 at 14:40
  • @MartinJames 1.35 is old and has no weighting on this question. – 111111 Apr 20 '12 at 15:06
  • @SteveTownsend I would certainly try it anyway, there are a lot of advantages in use the standard library. VC11 is around the cornor anyway with promises of regular updates. – 111111 Apr 20 '12 at 15:07
1

Boost.Thread

http://www.boost.org/libs/thread

authchir
  • 1,605
  • 14
  • 26
CodeClown42
  • 11,194
  • 1
  • 32
  • 67
1

boost::thread is the solution also std::thread in the new standard C++11.

authchir
  • 1,605
  • 14
  • 26
AlexTheo
  • 4,004
  • 1
  • 21
  • 35
1

As an alternative, a quite portable (*) and relatively non-intrusive library, well supported on g++ and (with an old version) on MSVC is OpenMP.

It is not standard C++, but OpenMP itself is a standard. It let's you paralllelize loops easily:

#pragma omp parallel for
for (int i=0; i!=x; ++i) {
}

It has also tasks and timing capabilities (and more), but in my opinion it's king discipline is above exampled parallel for.


(*) If you stay with the pragmas, it isn't unportable at all.

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130