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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

【linux多线程】c++多线程的几种创建方式

發(fā)布時間:2023/12/31 linux 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【linux多线程】c++多线程的几种创建方式 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

序言

  • 之前的文章介紹了 進(jìn)程和線程的基本概念,以及 C語言多線程的基本函數(shù)

  • 現(xiàn)對C++中多線程創(chuàng)建的幾種方式做一個總結(jié)(學(xué)習(xí)記錄)

1. 多線程

  • c++11中引入了線程類thread,頭文件

    #include <thread>
  • 創(chuàng)建多線程的方法

    std::thread threadName(函數(shù)名, 參數(shù)1, 參數(shù)2, ...) std::thread threadName(functionName, leftValueArg, rightValueArg, std::ref(refArg), std::cref(constRefArg));

    傳參可以傳左值、右值、引用使用std::ref、const引用使用std::cref;
    參數(shù)先copy或move到std::thread對象,再move給函數(shù)

  • 程序至少有一個線程(主線程),一旦創(chuàng)建了std::thread對象也就是在主線程外創(chuàng)建了一個子線程,其立刻開始運(yùn)行

2. 多線程的創(chuàng)建

常見的線程創(chuàng)建方式如下:

1. 普通函數(shù) 2. 成員函數(shù) 3. 仿函數(shù) 4. lambda函數(shù)
  • 線程創(chuàng)建方式1:普通函數(shù)
#include <iostream> #include <thread>void Function(int a, int& b) {a = 2;b = 2; }int main() {int a = 1;int b = 1;// create sub-threadstd::thread newThread(Function, a, std::ref(b));std::cout << a << " " << b << std::endl;// main thread waiting for sub-thread to finishnewThread.join();return 0; } // 編譯運(yùn)行 g++ --o main main.cpp -pthread
  • 線程創(chuàng)建方式2:成員函數(shù)
#include <iostream> #include <thread>class MyClass { public:void Function(const int& num){std::cout << "I am " << num << " yeas old" << std::endl;} };int main() {MyClass object;int num = 29;// create sub-threadstd::thread newThread(&MyClass::Function, &object, std::cref(num));// main thread waiting for sub-thread to finishnewThread.join();return 0; }
  • 線程創(chuàng)建方式3:仿函數(shù)
#include <iostream> #include <thread>class MyClass { public:void operator() (int& num){std::cout << "sub-thread start" << std::endl; num = 30;std::cout << "sub-thread end" << std::endl; } };int main() {std::cout << "main thread start" << std::endl;MyClass object;int num = 29;std::cout << "num = " << num << std::endl;// create sub-threadstd::thread newThread(object, std::ref(num));// std::thread newThread(MyClass(), std::ref(num)); 也可// main thread waiting for sub-thread to finishnewThread.join();std::cout << "num = " << num << std::endl;std::cout << "main thread end" << std::endl;return 0; } // 上面的例子,也可如下實(shí)現(xiàn) ... int main() {// MyClass object; // no need to create objectstd::thread newThread(MyClass(), std::ref(num));... }
  • 線程創(chuàng)建方式4:匿名函數(shù)lambda
#include <iostream> #include <thread>int main() {auto function = [](int a) { std::cout << a << '\n'; };// create sub-threadstd::thread newThread(function, 10);newThread.join();return 0; } // 或以如下方式調(diào)用lambda函數(shù) #include <iostream> #include <thread>int main() {// create sub-threadstd::thread newThread([](int a) {std::cout << a << std::endl;}, 10);newThread.join();return 0; }

?


參考文章:

多線程與單線程的對比
線程創(chuàng)建的幾種方式
cpp官網(wǎng)線程創(chuàng)建的幾種方式
ros多線程創(chuàng)建

created by shuaixio, 2022.04.30

總結(jié)

以上是生活随笔為你收集整理的【linux多线程】c++多线程的几种创建方式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。