<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`。 默認構造函數:? thread() noexcept;? 構造一個任何線程不執行的線程對象。 初始化函數: ~~~ template <class Fn, class... Args> explicit thread (Fn&& fn, Args&&... args); ~~~ 構造一個線程對象,可以開啟線程執行? 新執行的線程調用函數fn,并傳遞args作為參數? fn? 可以指向函數,指向成員,或是移動構造函數? args…? 傳遞給fn的參數,這些參數可以移動賦值構造。如果fn是一個成員指針,那么第一個args參數就必須是一個對象,或是引用,或是指向該對象的指針。 拷貝構造函數:? thread (const thread&) = delete;? 刪除構造的線程 現在介紹幾個成員函數:? **std::thread::join**? 該函數返回時,線程執行完成。? 當 a thread 調用Join方法的時候,MainThread 就被停止執行,直到 a thread 線程執行完畢。 ~~~ #include <iostream> // std::cout #include <thread> // std::thread, std::this_thread::sleep_for #include <chrono> // std::chrono::seconds void pause_thread(int n) { std::this_thread::sleep_for (std::chrono::seconds(n)); std::cout << "pause of " << n << " seconds ended\n"; } int main() { std::cout << "Spawning 3 threads...\n"; std::thread t1 (pause_thread,1); std::thread t2 (pause_thread,2); std::thread t3 (pause_thread,3); std::cout << "Done spawning threads. Now waiting for them to join:\n"; t1.join(); t2.join(); t3.join(); std::cout << "All threads joined!\n"; return 0; } //輸出 Output (after 3 seconds): Spawning 3 threads... Done spawning threads. Now waiting for them to join: pause of 1 seconds ended pause of 2 seconds ended pause of 3 seconds ended All threads joined! ~~~ **std::thread::detach**? 分離線程的對象,使它們從彼此獨立地執行所表示的線程。這兩個線程繼續沒有阻止,也沒有以任何方式同步。注意,當任一結束執行,其資源被釋放。 ~~~ #include <iostream> // std::cout #include <thread> // std::thread, std::this_thread::sleep_for #include <chrono> // std::chrono::seconds void pause_thread(int n) { std::this_thread::sleep_for (std::chrono::seconds(n)); std::cout << "pause of " << n << " seconds ended\n"; } int main() { std::cout << "Spawning and detaching 3 threads...\n"; std::thread (pause_thread,1).detach(); std::thread (pause_thread,2).detach(); std::thread (pause_thread,3).detach(); std::cout << "Done spawning threads.\n"; std::cout << "(the main thread will now pause for 5 seconds)\n"; // give the detached threads time to finish (but not guaranteed!): pause_thread(5); return 0; } //輸出 Output (after 5 seconds): Spawning and detaching 3 threads... Done spawning threads. (the main thread will now pause for 5 seconds) pause of 1 seconds ended pause of 2 seconds ended pause of 3 seconds ended pause of 5 seconds ended ~~~ 上一段代碼 深入理解: ~~~ #include <iostream> #include <thread> using namespace std; class Foo { void bar_i() { cout << "hello" << endl; } public: void bar() { auto func = std::bind(&Foo::bar_i, this); std::thread t(&Foo::bar_i, std::ref(*this)); t.join(); } }; int main() { Foo f; f.bar(); } ~~~ 如果你使用的是VS2015,那么恭喜你,上面的代碼你不會運行成功。
                  <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>

                              哎呀哎呀视频在线观看