日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

stdthread(2)创建

發布時間:2025/3/21 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 stdthread(2)创建 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.1 函數對象

class hello{ public:void operator()(){std::cout<<"Hello Concurrent World!"<<std::endl;} };std::thread t(hello());//error,編譯器會誤以為是hello函數std::thread t((hello())); //ok std::thread t{hello()}; //ok

1.2 例子

using namespace std; void fun1(int n) //初始化構造函數 { cout << "Thread " << n << " executing\n"; n += 10; this_thread::sleep_for(chrono::milliseconds(10)); } void fun2(int & n) //拷貝構造函數 { cout << "Thread " << n << " executing\n"; n += 20; this_thread::sleep_for(chrono::milliseconds(10)); } int test() { int n = 0; thread t1; //t1是一個空thread thread t2(fun1, n + 1); //按照值傳遞 t2.join(); cout << "n=" << n << '\n'; n = 10; thread t3(fun2, ref(n)); //引用 thread t4(move(t3)); //t4執行t3,t3不是thread t4.join(); cout << "n=" << n << '\n'; return 0; }

輸出:

Thread 1 executing n=0 Thread 10 executing n=30

1.3 lambda

void test() { auto fun = [](const char *str) {cout << str << endl; }; thread t1(fun, "hello world!"); thread t2(fun, "hello beijing!"); t1.join();t2.join(); }

輸出:

hello world! hello beijing!

1.4 可變參數

#include

int show(const char *fun, ...) { va_list ap;//指針 va_start(ap, fun);//開始 vprintf(fun, ap);//調用 va_end(ap); return 0; } int test() { thread t1(show, "%s %d %c %f", "hello world!", 100, 'A', 3.14159); t1.join(); return 0; }

輸出:

hello world! 100 A 3.141590

總結

以上是生活随笔為你收集整理的stdthread(2)创建的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。