Thread creation is easy. Call std::thread, and a new thread will be created. The thread gets a work package and starts it immediately. The creator of the thread (the Parent) has to take care of the created thread (the child). The Parent should wait until its child is done with its task or has to detach itself from its child. The child thread can get its payload task arguments by copy or by reference.
To be honest, that was too fast. So the details will follow.
Creation and execution of a thread
Now, more formal approach: a thread gets a Callable and starts it immediately.
This sentence needs a few notes.
- A Callable is an entity which behaves like a function. It can be a function, a function object or a lambda function.
- A function object is an instance of a class, for which the call operator is overloaded. The key difference between functions and function objects is, that a function object can have state.
- A lambda function (anonymous function) is a pure function body, without a name. It can be invoked just in place. A lambda function can capture its calling context. That's why they are often called closures.
After the theory a small example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
// createThread.cpp
#include <iostream>
#include <thread>
void helloFunction(){
std::cout << "Hello C++11 from function." << std::endl;
}
class HelloFunctionObject {
public:
void operator()() const {
std::cout << "Hello C++11 from a function object." << std::endl;
}
};
int main(){
std::cout << std::endl;
// thread executing helloFunction
std::thread t1(helloFunction);
// thread executing helloFunctionObject
HelloFunctionObject helloFunctionObject;
std::thread t2(helloFunctionObject);
// thread executing lambda function
std::thread t3([]{std::cout << "Hello C++11 from lambda function." << std::endl;});
// ensure that t1, t2 and t3 have finished before main terminates
t1.join();
t2.join();
t3.join();
std::cout << std::endl;
};
|
All threads - t1, t2 and t3 - write their messages to the console. The work package of thread t2 is a function object (line 10 - 15), the work package of thread t3 is a lambda function (line 29). In the lines 32 - 34 the Main thread or Parent waits, until his children are done.
Let's have a look at the output. This is more interesting.

The two programs execution results differ in two aspects. First, child threads will be executed in different order. Second, the output is a little bit of a mess. So, in the second run, the line break of the function helloFunction happens after the lambda function call.
What's next?
The next article will be about the lifetime of a thread. (Proofreader Alexey Elymanov)

Go to Leanpub/cpplibrary "What every professional C++ programmer should know about the C++ standard library". Get your e-book. Support my blog.
Comments
to do blogging and site-building.
provides feature based writing.
these kinds of things, thus I am going to let know her.
found any interesting article like yours. It's beautiful price sufficient for me.
In my opinion, if all webmasters and bloggers made just
right content as you probably did, the web will likely be
a lot more helpful than ever before.
of it. I have got you saved as a favorite to look at new stuff you post…
They are really convincing and can certainly work. Nonetheless, the posts are very short for beginners.
May you please prolong them a bit from subsequent time?
Thank you for the post.
RSS feed for comments to this post