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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] ## std::async 、std::promise、std::packaged_task以及std::future之間有什么聯系和區別 std::future是用于獲取將來共享狀態的運行結果或異常,相當于一個中間件,std::async 、std::promise、std::packaged_task都離不開它的幫助; std::packaged_task用于包裝可調用目標,以便異步執行任務; std::promise用于設置共享狀態的值,可以用于線程間交流,這個是比較特殊的。 std::async是最優雅地方式啟動任務異步執行;在多數情況下,**建議使用asyn開啟異步任務**,而不是使用packaged_task方式。 ## future 放到 thread 中執行 ``` #include <iostream> #include <future> #include <thread> int add(int a, int b) { std::this_thread::sleep_for(std::chrono::seconds(2)); return a + b; } int main() { std::packaged_task<int(int, int)> f(add); std::future<int> future = f.get_future(); std::thread(std::move(f), 1, 2).detach(); int i = future.get(); std::cout << i; return 0; } ``` ## std::async ``` #include <future> #include <iostream> #include <Windows.h> int main() { std::function<int(int, int)> f = [](int a, int b) { Sleep(10000); return a + b; }; std::future<int> res = std::async(f, 1, 2); std::cout << "--------------------"; // 阻塞,直到 res 能返回結果 std::cout << res.get(); std::cout << "=========="; return 0; } ``` ## task_lambda ``` #include <iostream> #include <cmath> #include <thread> #include <future> #include <functional> void task_lambda() { std::packaged_task<int(int, int)> task([](int a, int b) { return a + b; }); std::future<int> result = task.get_future(); task(2, 9); std::cout << "task_lambda:\t" << result.get() << '\n'; } int main() { task_lambda(); } ``` ## task_bind ``` #include <iostream> #include <cmath> #include <thread> #include <future> #include <functional> // unique function to avoid disambiguating the std::pow overload set int f(int x, int y) { return x + y; } void task_bind() { // 也可使用 // std::packaged_task<int()> task([] { return f(2, 11); }); std::packaged_task<int()> task(std::bind(f, 2, 11)); std::future<int> result = task.get_future(); task(); std::cout << "task_bind:\t" << result.get() << '\n'; } int main() { task_lambda(); // task_bind(); // task_thread(); } ``` ## task_thread ``` #include <iostream> #include <cmath> #include <thread> #include <future> #include <functional> // unique function to avoid disambiguating the std::pow overload set int f(int x, int y) { return x + y; } void task_thread() { std::packaged_task<int(int, int)> task(f); std::future<int> result = task.get_future(); std::thread task_td(std::move(task), 2, 10); task_td.join(); std::cout << "task_thread:\t" << result.get() << '\n'; } int main() { task_thread(); } ```
                  <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>

                              哎呀哎呀视频在线观看