C++11 多线程相关知识的学习
生活随笔
收集整理的這篇文章主要介紹了
C++11 多线程相关知识的学习
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
C++多線程類Thread(C++11)
- C++11中std命名空間將Boost庫中的Thread加入,Boost的多線程從準標準變為標準,這里將其用法整理復習,以demo的形式復習,還是喜歡看我自己寫的東西,符合我的個人邏輯
- 頭文件為#include<thread>,通過std::thread應用。就以Hello thread開始吧,需要注意的是1,join()函數和detach()函數的區別,2,數據同步操作mutex(需包含include<mutex>):互斥鎖
1、??普通函數多線程調用
(1)無參數函數
#include <thread> #include <iostream>void hello_thread() {std::cout << "Hello Thread!" << std::endl; }int main() {std::thread t1(hello_thread); t1.join(); // 主線程等待子線程執行結束之后,才會執行下一步的操作std::cout << "Main here" << std::endl;getchar();return 0; }(2)有參數函數
#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(); // 主線程等待子線程執行結束之后,才會執行下一步的操作std::cout << "Main here" << std::endl;getchar();return 0; }2、在類內部創建線程
(1)類內部函數為靜態函數
#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模式內部創建線程:?
3 、用類內部函數在類外部創建線程:
- 非常普通的類,只是用多線程調用其內部的函數
?join()和detach()的區別:
- join()的作用前面已經提到,主線程等待子線程結束方可執行下一步(串行),detach()是的子線程放飛自我,獨立于主線程并發執行,主線程后續代碼段無需等待??纯葱Ч?#xff1a;
(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);//主線程等待子線程運行結束之后才可以執行下一步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);//主線程等待子線程運行結束之后才可以執行下一步t1.detach();thread t2(msum,2,3);t2.detach();//主線程代碼cout << "Main here " << endl;getchar();return 0; }?數據同步(線程同時操作一個數據的安全性):
-
執行單個線程,上鎖、解鎖,維護了線程的安全性,防止線程之間交叉執行
線程暫停
- 從外部讓線程暫停,會引發很多并發問題,此處不做引申。這大概也是std::thread并沒有直接提供pause函數的原因。但有時線程在運行時,確實需要“停頓”一段時間怎么辦呢?可以使用std::this_thread::sleep_for或std::this_thread::sleep_until
線程停止
- 一般情況下當線程函數執行完成后,線程“自然”停止。但在std::thread中有一種情況會造成線程異常終止,那就是:析構。當std::thread實例析構時,如果線程還在運行,則線程會被強行終止掉,這可能會造成資源的泄漏,因此盡量在析構前join一下,以確保線程成功結束。
- 如果確實想提前讓線程結束怎么辦呢?一個簡單的方法是使用“共享變量”,線程定期地去檢測該量,如果需要退出,則停止執行,退出線程函數。使用“共享變量”需要注意,在多核、多CPU的情況下需要使用“原子”操作。
參考鏈接
- https://blog.csdn.net/ouyangfushu/article/details/80199140
總結
以上是生活随笔為你收集整理的C++11 多线程相关知识的学习的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 东软 软件工程1 软件危机 软件工程 软
- 下一篇: 程序员 面试笔记 C++ 程序设计的基础