C++ concurrent multithreading--creating threads

Table of contents

1-- Create threads based on thread

1-1--Create a thread using a function object

1-2--Using class objects to create threads

1-3--Create threads using lambda expressions


1-- Create threads based on thread

        Threads can be created using the thread standard library of C++: commonly used APIs include join(), detach(), joinable(), etc.;

        join() is generally used to block the main thread, so that the main thread needs to wait for the child thread to finish executing before ending;

        detach() is used to separate the sub-thread from the main thread. The main thread does not need to wait for the sub-thread to finish executing. When the main thread ends, the sub-thread will run in the background;

        joinable() is used to judge whether join() or detach() can be used, and returns true if it can be used, and false() if it cannot be used. When a child thread calls detach(), it cannot call join() again;

        Note: Add -lpthread parameter when compiling in linux environment (cmake plus target_link_libraries(main pthread))

1-1--Create a thread using a function object

# include <iostream>
# include <thread>

void myprint(){
    int i = 100;
    while(i > 0){
        std::cout << "thread print" << std::endl;
        i--;
    }
}

int main(){
    // 创建子线程对象
    std::thread thread1(myprint);
    
    int i = 100;
    while(i > 0){
        std::cout << "main print" << std::endl;
        i--;
    }

    thread1.join(); // 阻塞主线程,主线程需等待子线程执行完毕
    return 0;
}

1-2--Using class objects to create threads

# include <iostream>
# include <thread>

class myclass{
public:
    // 函数名必须为 operator, 且不能携带参数 
    void operator()(){
        int i = 100;
        while(i > 0){
        std::cout << "thread print" << std::endl;
        i--;
        }
    }
};

int main(){

    // 创建子线程对象
    myclass class1;
    std::thread thread1(class1);

    int i = 100;
    while(i > 0){
        std::cout << "main print" << std::endl;
        i--;
    }

    thread1.join(); // 阻塞主线程,主线程需等待子线程执行完毕
    return 0;
}

        When using detach(), you should avoid the error that all the variables of the main thread are recycled due to the completion of the main thread execution, so that the variables passed to the sub-thread become unknown variables, such as the following code:

        When the main thread creates a child thread, the reference passed to the index is passed. When detach() is used to separate the main thread and the child thread, if the main thread ends first, the index variable will be recycled. At this time, the index passed to the class object by reference will become unknown , thus causing errors and bugs, etc.;

        Therefore, when using detach(), you should try to avoid reference passing , and you can use ordinary passing instead , and reassign a copy of the object or variable of the main thread to the child thread;

// Error bug code
# include <iostream>
# include <thread>

class myclass{
public:
    myclass(int &index):my_i(index){}; // 用引用传递的 i 初始化 my_i

    // 函数名必须为 operator, 且不能携带参数 
    void operator()(){
        int i = 100;
        while(i > 0){
        std::cout << "thread print" << std::endl;
        i--;
        }
    }
private:
    int my_i;
};

int main(){

    // 创建子线程对象
    int index = 1;
    myclass class1(index);
    std::thread thread1(class1);

    int i = 100;
    while(i > 0){
        std::cout << "main print" << std::endl;
        i--;
    }

    thread1.detach(); // 阻塞主线程,主线程需等待子线程执行完毕
    return 0;
}

1-3--Create threads using lambda expressions

# include <iostream>
# include <thread>

int main(){

    auto lamthread = [](){
        int i = 100;
        while(i > 0){
            std::cout << "thread print" << std::endl;
            i--;
        }
    };

    std::thread thread1(lamthread);
    thread1.detach(); // 主线程和子线程分离

    int j = 100;
    while(j > 0){
        std::cout << "main print" << std::endl;
        j--;
    }

    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43863869/article/details/131731604