
- C 標準庫
- C 標準庫
- C++ 標準庫
- C++ 庫 - 首頁
- C++ 庫 - <fstream>
- C++ 庫 - <iomanip>
- C++ 庫 - <ios>
- C++ 庫 - <iosfwd>
- C++ 庫 - <iostream>
- C++ 庫 - <istream>
- C++ 庫 - <ostream>
- C++ 庫 - <sstream>
- C++ 庫 - <streambuf>
- C++ 庫 - <atomic>
- C++ 庫 - <complex>
- C++ 庫 - <exception>
- C++ 庫 - <functional>
- C++ 庫 - <limits>
- C++ 庫 - <locale>
- C++ 庫 - <memory>
- C++ 庫 - <new>
- C++ 庫 - <numeric>
- C++ 庫 - <regex>
- C++ 庫 - <stdexcept>
- C++ 庫 - <string>
- C++ 庫 - <thread>
- C++ 庫 - <tuple>
- C++ 庫 - <typeinfo>
- C++ 庫 - <utility>
- C++ 庫 - <valarray>
- C++ STL 庫
- C++ 庫 - <array>
- C++ 庫 - <bitset>
- C++ 庫 - <deque>
- C++ 庫 - <forward_list>
- C++ 庫 - <list>
- C++ 庫 - <map>
- C++ 庫 - <multimap>
- C++ 庫 - <queue>
- C++ 庫 - <priority_queue>
- C++ 庫 - <set>
- C++ 庫 - <stack>
- C++ 庫 - <unordered_map>
- C++ 庫 - <unordered_set>
- C++ 庫 - <vector>
- C++ 庫 - <algorithm>
- C++ 庫 - <iterator>
- C++ 高階庫
- C++ 庫 - <any>
- C++ 庫 - <barrier>
- C++ 庫 - <bit>
- C++ 庫 - <chrono>
- C++ 庫 - <cinttypes>
- C++ 庫 - <clocale>
- C++ 庫 - <condition_variable>
- C++ 庫 - <coroutine>
- C++ 庫 - <cstdlib>
- C++ 庫 - <cstring>
- C++ 庫 - <cuchar>
- C++ 庫 - <charconv>
- C++ 庫 - <cfenv>
- C++ 庫 - <cmath>
- C++ 庫 - <ccomplex>
- C++ 庫 - <expected>
- C++ 庫 - <format>
- C++ 庫 - <future>
- C++ 庫 - <flat_set>
- C++ 庫 - <flat_map>
- C++ 庫 - <filesystem>
- C++ 庫 - <generator>
- C++ 庫 - <initializer_list>
- C++ 庫 - <latch>
- C++ 庫 - <memory_resource>
- C++ 庫 - <mutex>
- C++ 庫 - <mdspan>
- C++ 庫 - <optional>
- C++ 庫 - <print>
- C++ 庫 - <ratio>
- C++ 庫 - <scoped_allocator>
- C++ 庫 - <semaphore>
- C++ 庫 - <source_location>
- C++ 庫 - <span>
- C++ 庫 - <spanstream>
- C++ 庫 - <stacktrace>
- C++ 庫 - <stop_token>
- C++ 庫 - <syncstream>
- C++ 庫 - <system_error>
- C++ 庫 - <string_view>
- C++ 庫 - <stdatomic>
- C++ 庫 - <variant>
- C++ STL 庫速查表
- C++ STL - 速查表
- C++ 程式設計資源
- C++ 程式設計教程
- C++ 有用資源
- C++ 討論
C++ 庫 - <mutex>
C++ 中的<mutex> 標頭檔案提供了一組用於管理多執行緒環境中共享資源訪問的工具。當多個執行緒嘗試同時訪問同一個資源時,可能會發生資料競爭。此庫透過允許控制對共享資源的訪問來幫助防止這些問題,確保在任何給定時間只有一個執行緒可以使用特定資源。
<mutex> 的工作原理是,當執行緒需要訪問資源時鎖定該資源,其他嘗試使用相同資源的執行緒必須等待當前執行緒解鎖互斥鎖。此庫還包括其他同步原語,如 recursive_mutex、timed_mutex 和 shared_mutex,它們提供了適合不同用例的特定功能。
包含 <mutex> 標頭檔案
要在您的 C++ 程式中包含 <mutex> 標頭檔案,您可以使用以下語法。
#include <mutex>
<mutex> 標頭檔案的函式
以下是 <mutex> 標頭檔案中所有函式的列表。
序號 | 函式及描述 |
---|---|
1 | lock
它鎖定指定的互斥鎖。 |
2 | try_lock
它嘗試鎖定互斥鎖。 |
3 | unlock
它解鎖互斥鎖。 |
4 | native_handle
它返回底層實現。 |
保護共享資料
在以下示例中,我們將使用互斥鎖來確保一次只有一個執行緒可以訪問共享資料。
#include <iostream> #include <mutex> #include <thread> std::mutex a; int b = 0; void increment() { for (int x = 0; x < 1123; ++x) { a.lock(); ++b; a.unlock(); } } int main() { std::thread x1(increment); std::thread x2(increment); x1.join(); x2.join(); std::cout << "Result : " << b << std::endl; return 0; }
輸出
以上程式碼的輸出如下:
Result : 2246
自動鎖定和解鎖
考慮以下示例,我們將使互斥鎖自動鎖定和解鎖。
#include <iostream> #include <thread> #include <mutex> std::mutex a; int b = 0; void increment() { std::lock_guard < std::mutex > lock(a); ++b; } int main() { std::thread x1(increment); std::thread x2(increment); x1.join(); x2.join(); std::cout << "Result : " << b << std::endl; return 0; }
輸出
以下是以上程式碼的輸出:
Result : 2
廣告