Aslan986
Aslan986

Reputation: 10314

Portable C++ multithreading

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.

Upvotes: 3

Views: 3263

Answers (4)

wholerabbit
wholerabbit

Reputation: 11536

Boost.Thread

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

Upvotes: 1

AlexTheo
AlexTheo

Reputation: 4164

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

Upvotes: 1

Sebastian Mach
Sebastian Mach

Reputation: 39089

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.

Upvotes: 1

111111
111111

Reputation: 16148

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/

Upvotes: 12

Related Questions