<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國際加速解決方案。 廣告
                # std::future和std::promise 并行開發挺復雜的,特別是在試圖用好線程和鎖的過程中。如果要用到條件變量或std-atomics(一種無鎖開發方式),那就更復雜了。C++0x提供了future和promise來簡化任務線程間的返回值操作;同時為啟動任務線程提供了packaged_task以方便操作。其中的關鍵點是允許2個任務間使用無(顯式)鎖的方式進行值傳遞;標準庫幫你高效的做好這些了。基本思路很簡單:當一個任務需要向父線程(啟動它的線程)返回值時,它把這個值放到promise中。之后,這個返回值會出現在和此promise關聯的future中。于是父線程就能讀到返回值。更簡單點的方法,參看async()。 標準庫中提供了3種future:普通future和為復雜場合使用的shared_future和atomic_future。在本主題中,只展示了普通future,它已經完全夠用了。如果我們有一個future f,通過get()可以獲得它的值: ``` X v = f.get(); // if necessary wait for the value to get computed ``` 如果它的返回值還沒有到達,調用線程會進行阻塞等待。要是等啊等啊,等到花兒也謝了的話,get()會拋出異常的(從標準庫或等待的線程那個線程中拋出)。 如果我們不需要等待返回值(非阻塞方式),可以簡單詢問一下future,看返回值是否已經到達: ``` if (f.wait_for(0)) { // there is a value to get() // do something } else { // do something else } ``` 但是,future最主要的目的還是提供一個簡單的獲取返回值的方法:get()。 promise的主要目的是提供一個”put”(或”get”,隨你)操作,以和future的get()對應。future和promise的名字是有歷史來歷的,是一個雙關語。感覺有點別扭?請別怪我。 promise為future傳遞的結果類型有2種:傳一個普通值或者拋出一個異常 ``` try { X res; // compute a value for res p.set_value(res); } catch (…) { // oops: couldn’t compute res p.set_exception(std::current_exception()); } ``` 到目前為止還不錯,不過我們如何匹配future/promise對呢?一個在我的線程,另一個在別的啥線程中嗎?是這樣:既然future和promise可以被到處移動(不是拷貝),那么可能性就挺多的。最普遍的情況是父子線程配對形式,父線程用future獲取子線程promise返回的值。在這種情況下,使用async()是很優雅的方法。 packaged_task提供了啟動任務線程的簡單方法。特別是它處理好了future和promise的關聯關系,同時提供了包裝代碼以保證返回值/異常可以放到promise中,示例代碼: ``` void comp(vector& v) { // package the tasks: // (the task here is the standard // accumulate() for an array of doubles): packaged_task pt0{std::accumulate}; packaged_task pt1{std::accumulate}; auto f0 = pt0.get_future(); // get hold of the futures auto f1 = pt1.get_future(); pt0(&v[0],&v[v.size()/2],0); // start the threads pt1(&[v.size()/2],&v[size()],0); return f0.get()+f1.get(); // get the results } ``` 參看: * Standard: 30.6 Futures [futures] * Anthony Williams: [Moving Futures – Proposed Wording for UK comments 335, 336, 337 and 338](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2888.html). N2888==09-0078. * Detlef Vollmann, Howard Hinnant, and Anthony Williams [An Asynchronous Future Value (revised)](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2627.html) N2627=08-0137. * Howard E. Hinnant: [Multithreading API for C++0X – A Layered Approach](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2094.html). N2094=06-0164\. The original proposal for a complete threading package.. (翻譯:interma)
                  <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>

                              哎呀哎呀视频在线观看