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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++11 多线程相关知识的学习

發(fā)布時間:2023/12/13 c/c++ 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++11 多线程相关知识的学习 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

C++多線程類Thread(C++11)

  • C++11中std命名空間將Boost庫中的Thread加入,Boost的多線程從準標準變?yōu)闃藴?#xff0c;這里將其用法整理復(fù)習(xí),以demo的形式復(fù)習(xí),還是喜歡看我自己寫的東西,符合我的個人邏輯
  • 頭文件為#include<thread>,通過std::thread應(yīng)用。就以Hello thread開始吧,需要注意的是1,join()函數(shù)和detach()函數(shù)的區(qū)別,2,數(shù)據(jù)同步操作mutex(需包含include<mutex>):互斥鎖

1、??普通函數(shù)多線程調(diào)用

(1)無參數(shù)函數(shù)

#include <thread> #include <iostream>void hello_thread() {std::cout << "Hello Thread!" << std::endl; }int main() {std::thread t1(hello_thread); t1.join(); // 主線程等待子線程執(zhí)行結(jié)束之后,才會執(zhí)行下一步的操作std::cout << "Main here" << std::endl;getchar();return 0; }

(2)有參數(shù)函數(shù)

#include <thread> #include <iostream>using namespace std;int msum(int a, int b) {int c = a + b;cout << a << " + " << b << " = " << c << endl;return c; }int main() {std::thread t1(msum,1,2);t1.join(); // 主線程等待子線程執(zhí)行結(jié)束之后,才會執(zhí)行下一步的操作std::cout << "Main here" << std::endl;getchar();return 0; }

2、在類內(nèi)部創(chuàng)建線程

(1)類內(nèi)部函數(shù)為靜態(tài)函數(shù)

#include <thread> #include <iostream>using namespace std;class HelloThread{public:static void hellothread(){cout << "Hello World, I‘m a thread " << endl;}static void start(){thread thread(hellothread);thread.join();}};int main(){HelloThread::start();getchar();return 0; }
  • 在這里start()和hellothread()方法都必須是static方法。?

(2)在Singleton模式內(nèi)部創(chuàng)建線程:?

3 、用類內(nèi)部函數(shù)在類外部創(chuàng)建線程:

  • 非常普通的類,只是用多線程調(diào)用其內(nèi)部的函數(shù)
#include <thread> #include <iostream>using namespace std;class HelloWorld{public:void hello(int year){cout << "I am " << year << " years old!" << endl;} };int main(){HelloWorld obj;thread thrd(&HelloWorld::hello,&obj,26);thrd.join();getchar();return 0; }

?join()和detach()的區(qū)別:

  • join()的作用前面已經(jīng)提到,主線程等待子線程結(jié)束方可執(zhí)行下一步(串行),detach()是的子線程放飛自我,獨立于主線程并發(fā)執(zhí)行,主線程后續(xù)代碼段無需等待。看看效果:

(1)join()

#include <thread> #include <iostream>using namespace std;void hello_thread() {std::cout << "Hello Thread!" << std::endl; }int msum(int a, int b) {int c = a + b;cout << a << " + " << b << " = " << c << endl;return c; }int main(){thread t1(hello_thread);//主線程等待子線程運行結(jié)束之后才可以執(zhí)行下一步t1.join();thread t2(msum,2,3);t2.join();//主線程代碼cout << "Main here " << endl;getchar();return 0; }

(2)detach()

#include <thread> #include <iostream>using namespace std;void hello_thread() {std::cout << "Hello Thread!" << std::endl; }int msum(int a, int b) {int c = a + b;cout << a << " + " << b << " = " << c << endl;return c; }int main(){thread t1(hello_thread);//主線程等待子線程運行結(jié)束之后才可以執(zhí)行下一步t1.detach();thread t2(msum,2,3);t2.detach();//主線程代碼cout << "Main here " << endl;getchar();return 0; }

?數(shù)據(jù)同步(線程同時操作一個數(shù)據(jù)的安全性):

  • 執(zhí)行單個線程,上鎖、解鎖,維護了線程的安全性,防止線程之間交叉執(zhí)行

#include <thread> #include <iostream> #include <mutex> using namespace std; std::mutex mt; int data1 = 1;void addmethod(int a){mt.lock();data1 += a;cout << "add data = " << data1 << endl;mt.unlock(); } void multi(int a){mt.lock();data1 *= a;cout << "multi data = " << data1 << endl;mt.unlock(); } void hello_thread() {std::cout << "Hello Thread!" << std::endl; }int msum(int a, int b) {int c = a + b;cout << a << " + " << b << " = " << c << endl;return c; }int main(){thread t1(addmethod,2);thread t2(multi,2);t1.detach();t2.detach();getchar();return 0; }

線程暫停

  • 從外部讓線程暫停,會引發(fā)很多并發(fā)問題,此處不做引申。這大概也是std::thread并沒有直接提供pause函數(shù)的原因。但有時線程在運行時,確實需要“停頓”一段時間怎么辦呢?可以使用std::this_thread::sleep_for或std::this_thread::sleep_until
#include <thread> #include <iostream> #include <mutex> #include <chrono> #include <ratio> using namespace std; std::mutex mt;void pausable(){//sleep 500毫秒this_thread::sleep_for(chrono::milliseconds(500));//sleep 到達指定的時間點this_thread::sleep_until(chrono::system_clock::now() + chrono::milliseconds(500)); } int main(){std::thread thread1(pausable);thread1.join();return 0; }

線程停止

  • 一般情況下當(dāng)線程函數(shù)執(zhí)行完成后,線程“自然”停止。但在std::thread中有一種情況會造成線程異常終止,那就是:析構(gòu)。當(dāng)std::thread實例析構(gòu)時,如果線程還在運行,則線程會被強行終止掉,這可能會造成資源的泄漏,因此盡量在析構(gòu)前join一下,以確保線程成功結(jié)束。
  • 如果確實想提前讓線程結(jié)束怎么辦呢?一個簡單的方法是使用“共享變量”,線程定期地去檢測該量,如果需要退出,則停止執(zhí)行,退出線程函數(shù)。使用“共享變量”需要注意,在多核、多CPU的情況下需要使用“原子”操作。

參考鏈接

  • https://blog.csdn.net/ouyangfushu/article/details/80199140

總結(jié)

以上是生活随笔為你收集整理的C++11 多线程相关知识的学习的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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