Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 std::thread vs windows CreateThread [closed]

Which option is better for creating (and managing) threads under Visual C++: C++11 std::thread or WinAPI functions (such as CreateThread, _beginthreadex, etc.) and why?

like image 501
KaMyLuS Avatar asked Aug 09 '14 17:08

KaMyLuS


People also ask

Does STD thread work in Windows?

std::thread is the C++11 way to do it, and on Microsoft platforms it makes use of ConCRT/ppl. std::thread has the advantage of working for Windows Store apps, where the other thread APIs are not available there.

What is CreateThread?

The CreateThread function creates a new thread for a process. The creating thread must specify the starting address of the code that the new thread is to execute.

What header file should you include for using C++11 multithreading capabilities?

Multithreading in C++ C++ 11 did away with all that and gave us std::thread. The thread classes and related functions are defined in the thread header file. std::thread is the thread class that represents a single thread in C++.

How do you close a thread in C++?

The C++11 does not have direct method to terminate the threads. The std::future<void> can be used to the thread, and it should exit when value in future is available. If we want to send a signal to the thread, but does not send the actual value, we can pass void type object.


1 Answers

Portability

std::thread is new to C++11 standard - with it, you can write portable code in C++ across compilers supporting C++11. You can feel the future in it.

It is based on boost::thread, which supports older compilers not supporting C++11 - which makes porting to other platforms even easier.

If you need to use platform specific tricks, std::thread::native_handle is the way to go.

CreateThread is specific to WinAPI, this implies writing non-portable code. Also, this API is quite old and more inconvenient to use.

RAII

WinAPI is a C API which does not encourage modern C++ good practices. Every threading primitive you create, you must later destroy manually.

This is not the case for thread library in C++11, and this makes higher-level abstractions easier to write. While std::thread is still fairly low-level (either you .join() or .detach() your thread, or the thread destructor will terminate your program), C++11 threading library has std::lock_guard and other lock classes for supporting RAII for mutexes.

While C++11 has some higher-level abstractions, like std::async for launching functions asynchronously, it does not provide other abstractions like threadpools, so you may want to use other libraries.

Type safety

WinAPI can only call function pointers with specific signature - which is prone to bugs related to type safety, lifetime of objects and mismanaging memory.

std::thread can call any callable object:

// call free-standing function in a separate thread
std::thread first(func);

// call free-standing function with arguments (1, 2), in a separate thread
std::thread second(func, 1, 2); 

// call static member function in a separate thread
std::thread third(&A::static_memfun); 

// call non-static member of a temporary in a separate thread
std::thread fourth(&A::memfun, A());

//call std::function in a separate thread
std::function<void(int)> callback = std::bind(func, 1, _1);
std::thread fifth(callback, 2);

// call a function object
Functor f;
std::thread sixth(f);

TL;DR: There is no reason to use WinAPI threads as the main threading mechanism in new C++ code.

like image 108
milleniumbug Avatar answered Oct 04 '22 05:10

milleniumbug