Thread Creation

Contents[Show]

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.

 createThread

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)

 

 

 

 

 

title page smalltitle page small Go to Leanpub/cpplibrary "What every professional C++ programmer should know about the C++ standard library".   Get your e-book. Support my blog.

Comments   

0 #1 muban777.com 2016-07-09 18:42
Some genuinely good blog posts on this web site, thank you for contribution.
Quote
0 #2 JasaPoker.net 2016-09-17 22:42
Peculiar article, totally what I needed.
Quote
0 #3 taruhan bola 2016-09-23 05:09
This piece of writing gives clear idea designed for the new users of blogging, that actually how
to do blogging and site-building.
Quote
0 #4 Get coupon now 2016-10-07 22:40
I go to see everyday a few web pages and websites to read posts, except this blog
provides feature based writing.
Quote
0 #5 Bandar Bola ternama 2016-11-23 08:06
continuously i used to read smaller posts which as well clear their motive, and that is also happening with this piece of writing which I am reading now.
Quote
0 #6 Bandar Bola Terbesar 2016-11-27 00:48
Wow, this paragraph is good, my younger sister is analyzing
these kinds of things, thus I am going to let know her.
Quote
0 #7 agen poker terbesar 2016-11-28 15:11
I've been surfing on-line more than 3 hours lately, but I by no means
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.
Quote
0 #8 Indo-bet8 2016-12-17 01:47
I needed to thank you for this excellent read!! I certainly enjoyed every bit
of it. I have got you saved as a favorite to look at new stuff you post…
Quote
0 #9 Amos 2016-12-17 05:32
I do trust all the ideas you've introduced to your 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.
Quote

Add comment


My Newest E-Book

Latest comments

Subscribe to the newsletter (+ pdf bundle)

Blog archive

Source Code

Visitors

Today 89

All 333380

Currently are 142 guests and no members online