C++11 多线程相关知识的学习
生活随笔
收集整理的這篇文章主要介紹了
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ù)
?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í)行
線程暫停
- 從外部讓線程暫停,會引發(fā)很多并發(fā)問題,此處不做引申。這大概也是std::thread并沒有直接提供pause函數(shù)的原因。但有時線程在運行時,確實需要“停頓”一段時間怎么辦呢?可以使用std::this_thread::sleep_for或std::this_thread::sleep_until
線程停止
- 一般情況下當(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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 东软 软件工程1 软件危机 软件工程 软
- 下一篇: s3c2440移植MQTT