C ++ _多線程筆記


#include<iostream>
#include <thread>//創建線程需要添加的頭文件

using namespace std;
/*thread join(阻塞:主等子) detach(主子分離) */
int main ()
{
    
    
    
    return 0;
}


第三節:線程傳參詳解,detach()大坑,成員函數做線程函數
    (1)傳遞臨時對象作為線程函數
        (1.1)要避免的陷阱(解釋1)
        (1.2)要避免的陷阱(解釋2)
        (1.3)總結
    (2)臨時對象作為線程參數繼續講
        (2.1)線程ID的概念
        (2.2)臨時對象構造時機抓捕
    (3)傳遞類對象、智能指針作為線程參數
    (4)用成員函數指針做線程函數





第四節:創建多個線程、數據共享問題分析、案列代碼
    (1)創建和等待多個線程
    (2)數據共享問題分析
        (2.1)只讀的數據-->是安全穩定的,不需要特別的處理手段,直接讀就可以。
        (2.2)有讀有寫
        (2.3)其他案例
    (3)共享數據的保護案例代碼


代碼編寫:
創建和等待多個線程:
-----------------------------------------------------------------------------------------------------------
                //線程入口函數(多個線程的入口函數)
                void myPrint(int inum)
                {
                    cout<<"myPrint線程開始執行了!!!!,線程編號 = "<<inum << endl;
                    //......干各種事情
                    
                    
                    cout<< "myPrint線程結束執行了!!!!,線程編號 = "<<inum <<endl;
                    return ;
                    
                }


                int main ()
                {
                    //一、創建和等待多個線程
                    vector <thread> mythreads;
                    //創建10個線程,線程入口函數同一使用myPrint
                    for (int i = 0;i< 10;i++)
                    {
                        mythreads.push_back(thread(myPrint,i));//創建10 個線程,同時這10 個線程已近開始執行
                    }
                    
                    for(auto iter = mythreads.begin();iter != mythreads.end();++iter)
                    {
                        iter->join();//等待10 個線程都返回
                    }
                    
                    cout << "I LOVe "<<endl;
                }
--------------------------------------------------------------------------------------------------------
    總結a:多個線程執行順序是亂的,跟操作系統內部對線程的運行調度機制有關
        b:主線程等待所有子線程運行結束,最后主線程結束,老師推薦這種join的寫法,跟容易寫出穩定的程序,
        c:咱們把thread對象放入容器管理,看起來像個thread對象數組,這對我們一次創建大量的線程並對大量線程進行管理很方便。
    

第五節:互斥量概念、用法、死鎖演示及解決詳解
(1)互斥量的基本概念
(2)互斥量的用法
    (2.1)lock \unlock
     (2.2) std :: lock_guard ()類模板-->自動上解鎖
(3)死鎖
    (3.1)死鎖演示
    (3.2)死鎖的一般解決方案-->順序一致
    (3.3)std::lock() 函數模板
    (3.4)std::lock_guardde std::adopt_lock參數
    
    
第六節:unique_lock 詳解
    (1)unique_lock 取代lock_quard
    (2)unique_lock的第二個參數
        (2.1)std::adopt_lock
        (2.2)std::try_to_lock
        (2.3)std::defer_lock
    (3)unique_lock的成員函數
        (3.1)lock()
        (3.2)unlock()
        (3.3)try_lock()
        (3.4)release()
    (4)unique_lock 所有權的傳遞
    
第七節 單例設計模式共享數據分析、解決、call_once
    (1)設計模式大概談
    (2)單例設計模式
    (3)單例設計模式共享數據問題分析,解決
    (4)std::call_once()


第八節 conditition_variable 、wait、notify_one 、notify_all
    (1)條件變量std::conditition_variable、 wait()、 notify_one()
    (2)上述代碼深入思考
    (3)notify_all()
    
第九節 async 、future 、packaged_task、promise
    (1)std::async、 std::future創建后台任務並返回值
    (2)std::packaged_task
    (3)std::promise
    (4)小結

第十節 future 其他的成員函數 、 shared_future 、 atomic
    (1)std::future的其他成員函數
    (2)std::shared_future
    (3)原子操作std::atomic
        (3.1)原子操作概念引出范例
        (3.2)基本的std::atomic 用法范例
        (3.3)老師的心得
第十一節 std::atomic續談、std::async 深入談
        (1)原子操作std::atomic續談
        (2)std::async 深入談
            (2.1)std::async 參數詳述
            (2.2)std::async 和std::thread 的區別
            (2.3)std::async 不確定性問題的解決

第十二節Windows臨界區、其他各種mutex互斥量
    (1)Windows臨界區
    (2)多次進入臨界區試驗
    (3)自動析構技術
    (4)recursive_mutex 遞歸的獨占互斥量
    (5)帶超時的互斥量std::timed_mutex和std::recursive_timed_mutex
    
    
第十三節 補充知識、線程池淺談、數量談、總結
    (1)補充一些知識點
        (1.1)虛假喚醒
        (1.2)atomic
    (2)淺談線程池
        (2.1)場景設想
        (2.2)實現方式
    (3)線程創建數量談
    (4)C++多線程總結


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM