Threads
To launch a thread in C++, you have to include the <thread>
header.
Thread Creation
A thread std::thread
represents an executable unit. This executable unit, which the thread immediately starts, gets its work package as a callable unit. A thread is not copy-constructible or copy-assignable but move-constructible or move-assignable.
A callable unit is an entity that behaves like a function. Of course, it can be a function but also a function object, or a lambda function. The return value of the callable unit is ignored.
After discussing theory, here is a small example.
1
// createThread.cpp
2
3
#include
<iostream>
4
#include
<thread>
5
6
void
helloFunction
(){
7
std
::
cout
<<
"Hello from a function."
<<
std
::
endl
;
8
}
9
10
class
HelloFunctionObject
{
11
public
:
12
void
operator
()()
const
{
13
std
::
cout
<<
"Hello from a function object."
<<
std
:...