zoukankan      html  css  js  c++  java
  • c++11多线程---线程入口函数

    1、普通函数(线程入口)

    #include <thread>
    #include <iostream>
    
    void hello(const char *name) {
        std::cout << "Hello " << name << std::endl;
    }
    
    int main() {
        std::thread thread(hello, "C++11"); //函数名、参数
        thread.join();    //等待线程执行完毕
    
        return 0;
    }

    2、类成员函数(线程入口)

    #include <thread>
    #include <iostream>
    
    class Greet
    {
        const char *owner = "Greet";
    public:
        void SayHello(const char *name) {
            std::cout << "Hello " << name << " from " << this->owner << std::endl;
        }
    };
    int main() {
        Greet greet;
    
        std::thread thread(&Greet::SayHello, &greet, "C++11"); //传入成员函数地址、 类对象地址、参数
        thread.join();
    
        return 0;
    }
    //输出:Hello C++11 from Greet

    https://www.jianshu.com/u/88ad4f76eb79

  • 相关阅读:
    Redis基础
    Windows 10 中 安装 RabbitMQ
    Nginx
    第二章-矩阵
    第一章-行列式
    第六章-微分方程
    第五章-多元函数
    第四章-定积分
    第三章-不定积分
    第二章-导数
  • 原文地址:https://www.cnblogs.com/lovebay/p/11579041.html
Copyright © 2011-2022 走看看