C++线程的创建

前言

线程创建很容易,直接调用std::thread,就创建一个新线程了。该线程拿到任务后立即开始执行。
线程的创建者(父线程)必须管理创建的线程(子线程),应该等到子线程完成其任务或者让子线程从自己身上脱离。子线程可以通过复制或引用获取任务执行的参数。


创建和执行线程

现在,更正式的方法创建线程:一个线程获得一个Callable后立即启动它。

Callable是一个行为类似于一个函数的实体。它可以是一个函数,一个函数对象或lambda函数。
函数对象是类的实例,调用操作符()被重载。函数和函数对象之间的关键区别在于,函数对象可以具有状态。
lambda函数(匿名函数)是一个纯函数体,没有名字。它可以在适当的位置调用。lambda函数可以捕获它的调用上下文。这就是为什么他们经常被称为闭包。


不废话了,看个例子吧:

// 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;

    // 线程执行函数 helloFunction
    std::thread t1(helloFunction);

    // 线程执行函数对象 helloFunctionObject
    HelloFunctionObject helloFunctionObject;
    std::thread t2(helloFunctionObject);

    // 线程执行 lambda function
    std::thread t3([] 
    {
        std::cout << "Hello C++11 from lambda function." << std::endl; 
    });

    // 确保 t1, t2 and t3 在main函数结束之前结束
    t1.join();
    t2.join();
    t3.join();

    std::cout << std::endl;
};

所有线程t1、t2和t3将它们的输出写入控制台。
线程t2的工作包任务是一个函数对象,线程t3的工作包任务是lambda函数。
主线程或父进程等待,直到它的子线程完成为止。
让我们看一下输出:
这里写图片描述
这两个程序的执行结果在两个方面有所不同。首先,子线程将以不同的顺序执行。其次,输出有点乱。因此,在第二次运行中,函数helloFunction的换行符发生在lambda函数调用之后。


原文地址:

http://www.modernescpp.com/index.php/thread-creation

猜你喜欢

转载自blog.csdn.net/y396397735/article/details/81008086