How to start a thread in a C + + program _c language

Source: Internet
Author: User
Tags thread class unique id

C++11 introduces a new line threading that includes tools to start and manage threads, and provides a way to synchronize (mutex, lock, and atomic variables), and I will try to introduce you to this new line threading.

If you want to compile the code in this article, you need at least one compiler that supports C++11, I'm using GCC 4.6.1, and I need to use-c++0x or-c++11 parameters to enable c++11 support.

Start thread

Starting a thread in c++11 is very simple, you can use Std:thread to create a thread instance, the creation will start automatically, only need to pass a pointer to execute the function, please see the following Hello World code:


#include <thread>
#include <iostream>
 
void Hello () {
  std::cout << "Hello from Thread" << Std::endl;
}
 
int main () {
  std::thread t1 (hello);
  T1.join ();
 
  return 0;
}


All thread-related methods are defined in the thread header file, and it is interesting that we call the join () function in the above code to force the main thread to wait for execution to end before exiting. If you do not write join () This line of code, the possible result is to print the Hello from thread and a new line, or there may not be a new row. Because the main thread may be returned before the thread has finished executing.

Thread identification

Each thread has a unique ID to identify a different thread, and the Std:thread class has a get_id () method that returns a unique number for the corresponding thread, and you can access the current thread instance through Std::this_thread, and the following example shows how to use this ID:

#include <thread>
#include <iostream>
#include <vector>
 
void Hello () {
  std::cout << "Hello from Thread" << std::this_thread::get_id () << Std::endl;
}
 
int main () {
  std::vector<std::thread> threads;
 
  for (int i = 0; i < 5; ++i) {
    threads.push_back (hello);
  }
 
  for (auto& thread:threads) {
    thread.join ();
  }
 
  return 0;
}


Start each thread in turn, and then save it in a vector container, and the results of the program execution are unpredictable, such as:

  Hello from thread 140276650997504
  hello to thread 140276667782912
  Hello from thread 140276659390208
  Hello from thread 140276642604800
  Hello from thread 140276676175616

It may also be:

  Hello from thread hello from thread hello to thread 139810974787328Hello from thread 139810983180032Hello from Thread
   139810966394624
  139810991572736
  139810958001920

or other results, because the execution of multiple threads is interleaved. You have absolutely no way to control the order in which threads are executed (otherwise, why do they need threads?) )


When the thread is going to execute the code a little bit, you don't have to create a function specifically for it, you can use lambda to define the code to execute, so the first example we can rewrite as:

#include <thread>
#include <iostream>
#include <vector>
 
int main () {
  std::vector <std::thread> Threads;
 
  for (int i = 0; i < 5; ++i) {
    threads.push_back (Std::thread ([] () {
      std::cout << "Hello from Thread" <& Lt std::this_thread::get_id () << Std::endl;
    });
 
  for (auto& thread:threads) {
    thread.join ();
  }
 
  return 0;
}

Here we use a lambda expression to replace the function pointer, and the result is the same.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.