Portable C ++ multithreading

I am writing a C ++ library that uses multithreading. As I develop it on Windows, I use the method and structure of the data in the "Windows.h" (eg CreateThread, HANDLE WINAPIetc.).

Now I would like to make this code executable on both Windows and Linux; can you suggest me some streaming implementations for this?

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

Thanks in advance.

+3
source share
4 answers

It is best to use the new std :: threads library, it is portable and standardized and written in a modern style.

 std::thread my_thread(my_func, my_param);

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

++ 11 (VC10 > gcc 4.4 ), std:: threads - boost:: threads, boost ( , Win32 Linux).

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

, , Intel TBB, ++, , std:: algorithmms

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

http://threadingbuildingblocks.org/

+11

boost:: thread - std:: thread ++ 11.

+1

Alternatively, a rather portable (*) and relatively non-intrusive library, well supported on g ++ and (with the old version) on MSVC, OpenMP .

This is not standard C ++, but OpenMP itself is the standard. This makes it easy to loop through:

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

He also has the ability to complete tasks and time (and much more), but, in my opinion, this kingโ€™s discipline is above parallel for parallelism.


(*) If you are left with pragmas, this is not entirely impossible.

+1
source

All Articles