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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM