<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                **一、事實1:只要用臨時構造的A類對象作為參數傳遞給線程,那么一定能夠在主線程執行完畢前把線程函數的第二個參數給構造出來** ``` #include "pch.h" #include <iostream> #include <thread> #include <string> using namespace std; class A { public : int m_i; //類型轉換構造函數,就可以吧一個int轉換成一個類A對象 A(int a) :m_i(a) { cout << "A構造函數執行" << endl; } A(const A &a) :m_i(a.m_i) { cout << "A拷貝構造函數執行" << endl; } ~A() { cout << "A析構函數" << endl; } }; //void myprint(const int &i, char *pmybuf) //void myprint(const int i, const std::string& pmybuf) void myprint(const int i, const A& pmybuf) { std::cout << i << std::endl; std::cout << &pmybuf << std::endl;//指針為detach時,絕對會有問題 return; } int main() { //一、傳遞臨時對象作為線程參數 //(1,1)要避免的陷阱 //1. int mvar = 1; int &mvary = mvar; char mybuf[] = "this is test"; //thread mytobj(myprint, mvar, mybuf);//事實上存在mybuf都被回收了(main函數執行完了)系統采用mybuf轉string(不可以) thread mytobj(myprint,mvar,A(mvar));//我們可以直接將mybuf轉換成string對象,這是一個可以保證在線程中用肯定有效的 //在創建線程的同時構造臨時對象的方法傳遞參數是可行的 mytobj.detach();//子線程和主線程分別執行 cout << "I Love You" << endl; std::cout << "Hello World!\n"; } ``` **二、臨時對象作為線程參數** a、線程id的概念:id是個數字,每個線程(主線程,子線程)實際上都有對應一個數字唯一,線程id可以用c++標準庫里的函數來獲取,std::this_thread::get_id()來獲取; b、臨時對象構造時機捕獲 **三、傳遞類對象,智能指針作為線程參數** a、std::ref函數;能把參數的真引用傳遞到函數中去;線程中不能使用detach ``` #include "pch.h" #include <iostream> #include <thread> #include <string> using namespace std; class A { public : int m_i; //類型轉換構造函數,就可以吧一個int轉換成一個類A對象 A(int a) :m_i(a) { cout << "A構造函數執行" << this << "線程id = " << std::this_thread::get_id() << endl; } A(const A &a) :m_i(a.m_i) { cout << "A拷貝構造函數執行" << this << "線程id = " << std::this_thread::get_id() << endl; } ~A() { cout << "A析構函數" << this << "線程id = " << std::this_thread::get_id() << endl; } }; //void myprint(const int &i, char *pmybuf) //void myprint(const int i, const std::string& pmybuf) //void myprint(const int i, const A& pmybuf) //如果沒有用到std:ref的話 變量引用作為實參就需要加const void myprint(const int i, A& pmybuf) //加了std:ref { pmybuf.m_i = 100;//在沒有加std::sef這個函數之前這個是沒有的用的傳遞任何引用都沒用 std::cout << "子線程的參數地址" << &pmybuf << "threadid = " << std::this_thread::get_id() << std::endl;//指針為detach時,絕對會有問題 return; } int main() { //**三、傳遞類對象,智能指針作為線程參數** // a、std::ref函數;用來修改子線程里面的臨時拷貝的對象的成員變量; int mvar = 1; A mvarbuf(10); thread mytobj(myprint,mvar, std::ref(mvarbuf)); //在創建線程的同時構造臨時對象的方法傳遞參數是可行的 mytobj.join();//子線程和主線程分別執行 cout << "主線程id = " << std::this_thread::get_id() << endl; std::cout << "Hello World!\n"; } ``` **使用智能指針作為參數以定不能使用detach;** ``` void myprint(unique_ptr<int> pzn) { return; } int main() { //智能指針 unique_ptr<int> myp(new int(10)); std::thread mytobj(myprint,std::move(myp));//需要使用std::move將智能指針傳遞到線程中qu mytobj.join(); std::cout << "Hello World!\n"; } ``` **四、用成員函數指針作為線程函數**
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看