Threads
To use the multithreading interface of C++ you need the 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 callable unit can be a function, a function object or a lambda function:
// threadCreate.cpp
...
#include
<thread>
...
using
namespace
std
;
void
helloFunction
(){
cout
<<
"function"
<<
endl
;
}
class
HelloFunctionObject
{
public
:
void
operator
()()
const
{
cout
<<
"function object"
<<
endl
;
}
};
thread
t1
(
helloFunction
);
// function
HelloFunctionObject
helloFunctionObject
;
thread
t2
(
helloFunctionObject
);
// function object
thread
t3
([]{
cout
<<
"lambda function"
;
});
// lambda function
Lifetime
The creator of a thread has to take care of the lifetime of its created thread. The executable unit of...