[a little c + + knowledge a day] 015: three methods of creating c++11 threads

Posted by ikscovski on Thu, 10 Feb 2022 10:07:15 +0100

1. Create a thread with an initial function

   note that when c + + runs an executable program (creates a process), it will automatically create a main thread. This main thread and the process live and die together. When the main thread ends, the process ends.

#include "pch.h"
#include <iostream>
#include<thread>
void print1()
{
    cout << "print1_1 Thread execution" << endl;
    cout << "print1_2 Thread execution" << endl;
    cout << "print1_3 Thread execution" << endl;
}
using namespace std;
int main()
{
    thread mythread1(print1);
    mythread1.join();
    //mythread1.detach();
    cout << "Main thread execution" << endl;
    return 0;
}

   thread mythread1 (print1) creates a thread mythread1, and print1 () is the initial function (execution function) of the thread. mythread1.join(); Block the main thread and wait for mythread1 to finish executing. This is recommended. mythread1.detach(); Separation, so that the main thread and thread mythread1 are separated. The main thread can finish executing first. If the main thread finishes executing, the sub thread will run in the c + + background. Once detach() is used, the object associated with the sub thread will lose its association with the main thread. At this time, the sub thread will stay in the c + + background and run. When the main thread finishes executing, The child thread will be handed over to the c + + runtime library for management. The runtime library will clean up the resources related to the thread (daemon thread). Detach() will cause the child thread to lose process control. Therefore, it is recommended not to use detach(), and it is recommended to use jion(). return 0; Indicates that the main thread has completed execution, indicating that the process is about to exit.

2. Create a thread with class object

#include "pch.h"
#include <iostream>
#include<thread>
class T
{
public:
    /*Case 1:
    int &it;//All threads in a process share the same block of memory (memory sharing). It is actually unsafe to use references in threads
    T(int &m_it) :it(m_it)
    {
        cout << "The constructor is executed "< < endl;
    }
    */
    //You can't use references. You should use them like this
    int it;
    T(int m_it) :it(m_it)
    {
        cout << "The constructor is executed" << endl;
    }
    T(const T &t) :it(t.it) {
        cout << "The copy constructor is executed" << endl;
    }
    ~T()
    {
        cout << "The destructor is executed" << endl;
    }
    void operator()()
    {
        cout << "it Value:" << it << endl;
    }
};
int main()
{
    int itm = 8;
    T t(itm);//Constructor called
    thread mythread2(t);
    mythread2.join();
    //mythread2.detach();
    cout << "Main thread execution" << endl;
    return 0;
}

  thread mythread2(t); Copy constructor called. mythread2.detach(); In case 1, detach() cannot be used here. Because the class member variable is a reference, the memory will be recycled after the main thread such as itm executes, so the variables printed by the sub thread are invalid. This is a major bug. It must be noted that the safest way is to use value passing directly to generate a copy, so that there will be no mistake in using detach (). Here's another question? Why use detach() when the main thread finishes executing, there is no problem with using t as a local object. T is supposed to be recycled by the system? A: Although the object t is gone (it will certainly be recycled), when creating a sub thread, the object t is copied to the sub thread, so there is no problem with detach() and the sub thread will continue to execute after the main thread is executed T is destroyed, but the object copied to the thread still exists.

3. Create a thread with lambda expression

#include <iostream>
#include<thread>
using namespace std;
int main()
{
    //Create a thread with a lambda expression
    auto mylamthread = [] {//This is a lambda expression
        cout << "My thread started executing" << endl;
        //......
        cout << "My thread execution is over" << endl;
    };
    thread mythread3(mylamthread);
    mythread3.join();
    cout << "End of main thread execution" << endl;
    return 0;//Indicates that the execution of the main thread ends, indicating that the process ends
}

joinable(), this function is used to judge whether join() and detach() can still be used. If join() or detach() has been used, detach() or join() functions can no longer be used. It will return a Boolean true. Otherwise, it will return a false

Welcome to deep learning deep learning and mathematics, WeChat daily official account, learning free learning of large data, AI and other related learning resources, classic and latest in-depth learning related papers, learning algorithms and other Internet skills, probability theory, linear algebra and other advanced mathematical knowledge review]

Topics: C++