示例詳解C++中的各種鎖
更新時間:2024年11月06日 08:53:45 作者:Arthurian
C++中常見的鎖包括互斥鎖、遞歸互斥鎖、讀寫鎖、定時互斥鎖、遞歸定時互斥鎖、自旋鎖和條件變量,互斥鎖用于防止多線程同時訪問共享資源,遞歸互斥鎖允許同一線程多次獲取鎖,讀寫鎖區(qū)分讀寫操作,提高并發(fā)性
在多線程開發(fā)中,經(jīng)常會遇到數(shù)據(jù)同步,很多情況下用鎖都是一個很好的選擇。C++中常用的鎖主要有下面幾種:
互斥鎖(std::mutex)
- 這是最基本的一種鎖。它用于保護(hù)共享資源,在任意時刻,最多只有一個線程可以獲取該鎖,從而訪問被保護(hù)的資源。當(dāng)一個線程獲取了互斥鎖后,其他試圖獲取該鎖的線程會被阻塞,直到持有鎖的線程釋放它。
- 例如,在一個多線程程序中,如果多個線程需要訪問和修改同一個全局變量,就可以使用互斥鎖來確保在同一時間只有一個線程能夠進(jìn)行修改操作,避免數(shù)據(jù)競爭導(dǎo)致的錯誤結(jié)果。
#include <iostream> #include <mutex> #include <thread> std::mutex m; int counter = 0; void increment() { m.lock(); counter++; std::cout << "Counter value in thread " << std::this_thread::get_id() << " is " << counter << std::endl; m.unlock(); } int main() { std::thread t1(increment); std::thread t2(increment); t1.join(); t2.join(); return 0; }
遞歸互斥鎖(std::recursive_mutex)
- 遞歸互斥鎖允許同一個線程多次獲取該鎖。它內(nèi)部會記錄鎖的獲取次數(shù),每獲取一次,計數(shù)加 1,每次釋放鎖時,計數(shù)減 1,當(dāng)計數(shù)為 0 時,鎖才真正被釋放,可供其他線程獲取。
- 假設(shè)在一個復(fù)雜的函數(shù)調(diào)用鏈中,函數(shù) A 調(diào)用函數(shù) B,函數(shù) B 又調(diào)用函數(shù) A,并且這些函數(shù)都需要訪問同一個受保護(hù)的資源。如果使用普通互斥鎖,就會出現(xiàn)死鎖,而遞歸互斥鎖就可以避免這種情況,因為它允許同一線程多次獲取鎖。
#include <iostream> #include <mutex> #include <thread> std::recursive_mutex rm; void recursiveFunction(int count) { rm.lock(); if (count > 0) { std::cout << "Recursive call with count = " << count << std::endl; recursiveFunction(count - 1); } rm.unlock(); } int main() { std::thread t(recursiveFunction, 3); t.join(); return 0; }
讀寫鎖(std::shared_mutex) C++17開始才有
- 讀寫鎖主要用于區(qū)分對共享資源的讀操作和寫操作。它有兩種獲取模式:共享模式(讀模式)和獨占模式(寫模式)。
- 多個線程可以同時以共享模式獲取讀寫鎖,這意味著它們可以同時讀取共享資源,而不會相互干擾。但是,當(dāng)一個線程要以獨占模式獲取讀寫鎖(進(jìn)行寫操作)時,其他線程(無論是讀操作還是寫操作)都不能獲取該鎖,直到寫操作完成并釋放鎖。這種機(jī)制在有大量讀操作和少量寫操作的場景下,可以提高程序的并發(fā)性能。例如,在一個緩存系統(tǒng)中,多個線程可能經(jīng)常讀取緩存中的數(shù)據(jù),只有在緩存數(shù)據(jù)需要更新時才會進(jìn)行寫操作,使用讀寫鎖可以很好地處理這種情況。
#include <iostream> #include <shared_mutex> #include <thread> #include <vector> std::shared_mutex smtx; int shared_data = 0; void read_data() { std::shared_lock<std::shared_mutex> lock(smtx); std::cout << "Read data: " << shared_data << std::endl; } void write_data(int new_value) { std::unique_lock<std::shared_mutex> lock(smtx); shared_data = new_value; std::cout << "Wrote data: " << shared_data << std::endl; } int main() { std::vector<std::thread> read_threads; for (int i = 0; i < 5; i++) { read_threads.push_back(std::thread(read_data)); } std::thread write_thread(write_data, 10); for (auto& t : read_threads) { t.join(); } write_thread.join(); return 0; }
定時互斥鎖(std::timed_mutex)
- 定時互斥鎖是
std::mutex
的擴(kuò)展。除了具備std::mutex
的基本功能外,它還允許線程在嘗試獲取鎖時設(shè)置一個超時時間。 - 如果在規(guī)定的超時時間內(nèi)無法獲取鎖,線程不會一直等待,而是可以執(zhí)行其他操作或者返回錯誤信息。這在一些對時間敏感的場景中非常有用,比如在一個實時系統(tǒng)中,線程不能因為等待一個鎖而無限期地阻塞,需要在一定時間后放棄獲取并進(jìn)行其他處理。
#include <iostream> #include <chrono> #include <thread> #include <mutex> std::timed_mutex tm; void tryLockFunction() { if (tm.try_lock_for(std::chrono::seconds(1))) { std::cout << "Acquired lock" << std::endl; std::this_thread::sleep_for(std::chrono::seconds(2)); tm.unlock(); } else { std::cout << "Could not acquire lock in time" << std::endl; } } int main() { std::thread t1(tryLockFunction); std::thread t2(tryLockFunction); t1.join(); t2.join(); return 0; }
遞歸定時互斥鎖(std::recursive_timed_mutex)
- 這是結(jié)合了遞歸互斥鎖和定時互斥鎖特點的一種鎖。它允許同一線程多次獲取鎖,并且在獲取鎖時可以設(shè)置超時時間。
- 當(dāng)一個線程多次獲取這種鎖后,需要釋放相同次數(shù)的鎖,鎖才會真正被釋放,并且在獲取鎖的過程中,如果在超時時間內(nèi)無法獲取,線程可以采取相應(yīng)的措施。
#include <iostream> #include <chrono> #include <thread> #include <mutex> std::recursive_timed_mutex rtm; void recursiveTryLockFunction(int count) { if (rtm.try_lock_for(std::chrono::seconds(1))) { std::cout << "Recursive acquired lock, count = " << count << std::endl; if (count > 0) { recursiveTryLockFunction(count - 1); } rtm.unlock(); } else { std::cout << "Could not recursively acquire lock in time" << std::endl; } } int main() { std::thread t(recursiveTryLockFunction, 3); t.join(); return 0; }
自旋鎖(通常用std::atomic_flag實現(xiàn))
- 自旋鎖是一種忙等待的鎖機(jī)制。當(dāng)一個線程嘗試獲取自旋鎖而鎖已經(jīng)被占用時,這個線程不會進(jìn)入阻塞狀態(tài),而是會不斷地檢查(“自旋”)鎖是否已經(jīng)被釋放。
- 自旋鎖在等待時間較短的情況下可能會有比較好的性能表現(xiàn),因為它避免了線程切換的開銷。但是,如果等待時間過長,由于線程一直在占用 CPU 資源進(jìn)行檢查,會導(dǎo)致 CPU 資源的浪費。一般在底層代碼或者對性能要求極高、等待時間預(yù)計很短的場景下使用。
#include <iostream> #include <atomic> #include <thread> std::atomic_flag spinLock = ATOMIC_FLAG_INIT; void criticalSection() { while (spinLock.test_and_set()) { // 自旋等待 } std::cout << "Entered critical section" << std::endl; // 臨界區(qū)操作 spinLock.clear(); } int main() { std::thread t1(criticalSection); std::thread t2(criticalSection); t1.join(); t2.join(); return 0; }
條件變量(std::condition_variable)配合互斥鎖用于同步(嚴(yán)格來說條件變量不是鎖,但常一起用于線程同步場景)
- 條件變量本身不是一種鎖,但它通常和互斥鎖一起使用,用于實現(xiàn)線程間的同步。它可以讓一個線程等待某個條件滿足后再繼續(xù)執(zhí)行。
- 例如,一個生產(chǎn)者 - 消費者模型中,消費者線程在緩沖區(qū)為空時可以使用條件變量等待,直到生產(chǎn)者線程生產(chǎn)出產(chǎn)品并通知消費者線程,這個過程中互斥鎖用于保護(hù)緩沖區(qū)這個共享資源,條件變量用于實現(xiàn)線程間的通信和同步。
#include <iostream> #include <thread> #include <mutex> #include <condition_variable> #include <queue> std::mutex mtx; std::condition_variable cv; std::queue<int> buffer; const int bufferSize = 5; void producer() { for (int i = 0; i < 10; ++i) { std::unique_lock<std::mutex> lock(mtx); while (buffer.size() == bufferSize) { cv.wait(lock); } buffer.push(i); std::cout << "Produced: " << i << std::endl; cv.notify_all(); } } void consumer() { for (int i = 0; i < 10; ++i) { std::unique_lock<std::mutex> lock(mtx); while (buffer.empty()) { cv.wait(lock); } int data = buffer.front(); buffer.pop(); std::cout << "Consumed: " << data << std::endl; cv.notify_all(); } } int main() { std::thread producerThread(producer); std::thread consumerThread(consumer); producerThread.join(); consumerThread.join(); return 0; }
到此這篇關(guān)于C++中的各種鎖的文章就介紹到這了,更多相關(guān)C++ 各種鎖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C字符串操作函數(shù)實現(xiàn)方法小結(jié)
這篇文章主要介紹了C字符串操作函數(shù)實現(xiàn)方法,實例總結(jié)了C語言字符串操作的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04