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?
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.
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.
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++.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With