C++ Boost 多執行緒(二),執行緒的引數傳遞
阿新 • • 發佈:2019-01-02
#include <iostream> #include <boost/thread.hpp> using namespace std; void func1(const int &id) { cout<<"func1 id : "<<id<<endl; } void func2(const int &id) { cout<<"func2 id : "<<id<<endl; } void func3(const int &id) { cout<<"func3 id : "<<id<<endl; } //執行緒的引數傳遞 int main() { boost::thread t1(func1, 11); boost::thread t2(func2, 22); boost::thread t3(func3, 33); t1.join(); t2.join(); t3.join(); system("pause"); return 0; }
#include <iostream> #include <boost/thread.hpp> using namespace std; void func1(const int &id) { cout<<"func1 id : "<<id<<endl; } struct MyThread { void operator()(const int &id) { cout<<"MyThread id : "<<id<<endl; } void func1(const int &id) { cout<<"MyThread::func1 id : "<<id<<endl; } }; //執行緒引數的傳遞方式 int main() { //普通函式 boost::thread t1(func1, 11); t1.join(); //函式物件 MyThread myThread; boost::thread t2(myThread, 22); t2.join(); //成員函式 boost::thread t3(&MyThread::func1, myThread, 33); t3.join(); //臨時物件 boost::thread t4(MyThread(), 44); t4.join(); //物件引用 boost::thread t5(boost::ref(myThread), 55); t5.join(); system("pause"); return 0; }