<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                繼續C++11的std::thread之旅! 下面討論如何**給線程傳遞參數**? 這個例子是傳遞一個string ~~~ #include <iostream> #include <thread> #include <string> void thread_function(std::string s) { std::cout << "thread function "; std::cout << "message is = " << s << std::endl; } int main() { std::string s = "Kathy Perry"; std::thread t(&thread_function, s); std::cout << "main thread message = " << s << std::endl; t.join(); return 0; } ~~~ 如果運行,我們可以從輸出結果看出傳遞成功了。 良好編程習慣的人都知道 傳遞引用的效率更高,那么我們該如何做呢?? 你也許會這樣寫: ~~~ void thread_function(std::string &s) { std::cout << "thread function "; std::cout << "message is = " << s << std::endl; s = "Justin Beaver"; } ~~~ 為了確認是否傳遞了引用?我們在線程函數后更改這個參數,但是我們可以看到,輸出結果并沒有變化,即不是傳遞的引用。 事實上, 依然是按值傳遞而不是引用。為了達到目的,我們可以這么做: ~~~ std::thread t(&thread_function, std::ref(s)); ~~~ 這不是唯一的方法: 我們可以使用move(): ~~~ std::thread t(&thread_function, std::move(s)); ~~~ 接下來呢,我們就要將一下線程的復制吧!!? 以下代碼編譯通過不了: ~~~ #include <iostream> #include <thread> void thread_function() { std::cout << "thread function\n"; } int main() { std::thread t(&thread_function); std::cout << "main thread\n"; std::thread t2 = t; t2.join(); return 0; } ~~~ 但是別著急,稍稍修改: ~~~ include <iostream> #include <thread> void thread_function() { std::cout << "thread function\n"; } int main() { std::thread t(&thread_function); std::cout << "main thread\n"; std::thread t2 = move(t); t2.join(); return 0; } ~~~ 大功告成!!! 再聊一個成員函數吧?**std::thread::get_id()**; ~~~ int main() { std::string s = "Kathy Perry"; std::thread t(&thread_function, std::move(s)); std::cout << "main thread message = " << s << std::endl; std::cout << "main thread id = " << std::this_thread::get_id() << std::endl; std::cout << "child thread id = " << t.get_id() << std::endl; t.join(); return 0; } Output: thread function message is = Kathy Perry main thread message = main thread id = 1208 child thread id = 5224 ~~~ 聊一聊**std::thread::hardware_concurrency()**? 獲得當前多少個線程: ~~~ int main() { std::cout << "Number of threads = " << std::thread::hardware_concurrency() << std::endl; return 0; } //輸出: Number of threads = 2 ~~~ 之前介紹過c++11的**lambda表達式**? 我們可以這樣使用: ~~~ int main() { std::thread t([]() { std::cout << "thread function\n"; } ); std::cout << "main thread\n"; t.join(); // main thread waits for t to finish return 0; } ~~~
                  <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>

                              哎呀哎呀视频在线观看