<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國際加速解決方案。 廣告
                [TOC] ## std::mutx - 但是在實際編寫代碼的過程中,最好不去直接調用成員函數, 因為調用成員函數就需要在每個臨界區的出口處調用`unlock()`, - 會產生異常 ``` #include <iostream> #include <mutex> #include <thread> int v = 1; void critical_section(int change_v) { // 使用 static ,想相當于全局變量 static std::mutex mtx; // 執行競爭操作 mtx.lock(); v = change_v; mtx.unlock(); } int main() { std::thread t1(critical_section, 2), t2(critical_section, 3); t1.join(); t2.join(); std::cout << v << std::endl; return 0; } ``` ## std::lock_guard - C++11 還為互斥量提供了一個 RAII 語法的模板類 std::lock_guard。 RAII 在不失代碼簡潔性的同時,很好的保證了代碼的異常安全性 - 此函數相當于 go的 defer,在函數退出時,觸發 在 RAII 用法下,對于臨界區的互斥量的創建只需要在作用域的開始部分, ``` #include <iostream> #include <mutex> #include <thread> int v = 1; void critical_section(int change_v) { static std::mutex mtx; std::lock_guard<std::mutex> lock(mtx); // 執行競爭操作 v = change_v; // 離開此作用域后 mtx 會被釋放 } int main() { std::thread t1(critical_section, 2), t2(critical_section, 3); t1.join(); t2.join(); std::cout << v << std::endl; return 0; } ``` ## std::unique_lock -推薦 而`std::unique_lock`則是相對于`std::lock_guard`出現的,`std::unique_lock`更加靈活,`std::unique_lock`的對象會以獨占所有權(沒有其他的`unique_lock`對象同時擁有某個`mutex`對象的所有權) 的方式管理`mutex`對象上的上鎖和解鎖的操作。所以在并發編程中,推薦使用`std::unique_lock` - unique_lock中的unique表示獨占所有權。 - unique_lock獨占的是mutex對象,就是對mutex鎖的獨占。 - `std::unique_lock`可以在聲明后的任意位置調用, 可以縮小鎖的作用范圍,提供更高的并發度 ``` #include <iostream> #include<thread> #include<mutex> #include <Windows.h> int number; void handle() { static std::mutex mux; std::unique_lock<std::mutex> lock(mux); number++; std::cout << number<<"\n"; lock.unlock(); // lock.unlock(); 如果下面要執行的共同代碼,可以不釋放, // todo code // 可以再次進行加鎖 lock.lock(); number++; std::cout << number << "\n"; } int main() { std::thread t1(handle), t2(handle); t1.join(); t2.join(); 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>

                              哎呀哎呀视频在线观看