97色伦色在线综合视频,无玛专区,18videosex性欧美黑色,日韩黄色电影免费在线观看,国产精品伦理一区二区三区,在线视频欧美日韩,亚洲欧美在线中文字幕不卡

攝影網(wǎng)站怎么做做網(wǎng)站的公司怎么找

鶴壁市浩天電氣有限公司 2026/01/24 14:04:09
攝影網(wǎng)站怎么做,做網(wǎng)站的公司怎么找,西安網(wǎng)站推廣都是怎么做的,長春網(wǎng)站建設(shè)排名mutex又稱互斥量#xff0c;C 11中與 mutex相關(guān)的類#xff08;包括鎖類型#xff09;和函數(shù)都聲明在 頭文件中#xff0c;所以如果 你需要使用 std::mutex#xff0c;就必須包含頭文件。 C11提供如下4種語義的互斥量#xff08;mutex#xff09; std::mutex#xff0…mutex又稱互斥量C 11中與 mutex相關(guān)的類包括鎖類型和函數(shù)都聲明在 頭文件中所以如果 你需要使用 std::mutex就必須包含頭文件。C11提供如下4種語義的互斥量mutexstd::mutex獨占的互斥量不能遞歸使用。std::time_mutex帶超時的獨占互斥量不能遞歸使用。std::recursive_mutex遞歸互斥量不帶超時功能。std::recursive_timed_mutex帶超時的遞歸互斥量。1.2.1 獨占互斥量std::mutexstd::mutex 介紹下面以 std::mutex 為例介紹 C11 中的互斥量用法。std::mutex 是C11 中最基本的互斥量std::mutex 對象提供了獨占所有權(quán)的特性——即不支持遞歸地 對 std::mutex 對象上鎖而 std::recursive_lock 則可以遞歸地對互斥量對象上鎖。std::mutex 的成員函數(shù)構(gòu)造函數(shù)std::mutex不允許拷貝構(gòu)造也不允許 move 拷貝最初產(chǎn)生的 mutex 對象是處于 unlocked 狀態(tài)的。lock()調(diào)用線程將鎖住該互斥量。線程調(diào)用該函數(shù)會發(fā)生下面 3 種情況(1). 如果該互斥量當(dāng)前沒 有被鎖住則調(diào)用線程將該互斥量鎖住直到調(diào)用 unlock之前該線程一直擁有該鎖。(2). 如果當(dāng) 前互斥量被其他線程鎖住則當(dāng)前的調(diào)用線程被阻塞住。(3). 如果當(dāng)前互斥量被當(dāng)前調(diào)用線程鎖 住則會產(chǎn)生死鎖(deadlock)。unlock() 解鎖釋放對互斥量的所有權(quán)。try_lock()嘗試鎖住互斥量如果互斥量被其他線程占有則當(dāng)前線程也不會被阻塞。線程調(diào)用該 函數(shù)也會出現(xiàn)下面 3 種情況(1). 如果當(dāng)前互斥量沒有被其他線程占有則該線程鎖住互斥量直 到該線程調(diào)用 unlock 釋放互斥量。(2). 如果當(dāng)前互斥量被其他線程鎖住則當(dāng)前調(diào)用線程返回 false而并不會被阻塞掉。(3). 如果當(dāng)前互斥量被當(dāng)前調(diào)用線程鎖住則會產(chǎn)生死鎖(deadlock)。范例1-2-mutex1//1-2-mutex1 #include iostream // std::cout #include thread // std::thread #include mutex // std::mutex volatile int counter(0); // non-atomic counter std::mutex mtx; // locks access to counter void increases_10k() { for (int i0; i10000; i) { // 1. 使用try_lock的情況 // if (mtx.try_lock()) { // only increase if currently not locked: // counter; // mtx.unlock(); // } // 2. 使用lock的情況 { mtx.lock(); counter; mtx.unlock(); } } } int main() { std::thread threads[10]; for (int i0; i10; i) threads[i] std::thread(increases_10k); for (auto th : threads) th.join(); std::cout successful increases of the counter counter std::endl; return 0; }1.2.2 遞歸互斥量std::recursive_mutex遞歸鎖允許同一個線程多次獲取該互斥鎖可以用來解決同一線程需要多次獲取互斥量時死鎖的問題。死鎖范例1-2-mutex2-dead-lock//死鎖范例1-2-mutex2-dead-lock #include iostream #include thread #include mutex struct Complex { std::mutex mutex; int i; Complex() : i(0){} void mul(int x) { std::lock_guardstd::mutex lock(mutex); i * x; } void div(int x) { std::lock_guardstd::mutex lock(mutex); i / x; } void both(int x, int y) { //lock_guard 構(gòu)造函數(shù)加鎖 析構(gòu)函數(shù)釋放鎖 std::lock_guardstd::mutex lock(mutex); mul(x); // 獲取不了鎖 div(y); } void init() { //lock_guard 構(gòu)造函數(shù)加鎖 析構(gòu)函數(shù)釋放鎖 std::lock_guardstd::mutex lock(mutex); sub_init(); } void sub_init() { std::lock_guardstd::mutex lock(mutex); } }; int main(void) { Complex complex; complex.both(32, 23); std::cout main finish ; return 0; }運行后出現(xiàn)死鎖的情況。在調(diào)用both時獲取了互斥量在調(diào)用mul時又要獲取互斥量但both的并沒有 釋放從而產(chǎn)生死鎖。使用遞歸鎖//遞歸鎖1-2-recursive_mutex1 #include iostream #include thread #include mutex struct Complex { std::recursive_mutex mutex; int i; Complex() : i(0){} void mul(int x) { std::lock_guardstd::recursive_mutex lock(mutex); i * x; } void div(int x) { std::unique_lockstd::recursive_mutex lock(mutex); i / x; } void both(int x, int y) { std::lock_guardstd::recursive_mutex lock(mutex); mul(x); div(y); } }; int main(void) { Complex complex; complex.both(32, 23); //因為同一線程可以多次獲取同一互斥量不會發(fā)生死鎖 std::cout main finish ; return 0; }雖然遞歸鎖能解決這種情況的死鎖問題但是盡量不要使用遞歸鎖主要原因如下1. 需要用到遞歸鎖的多線程互斥處理本身就是可以簡化的允許遞歸很容易放縱復(fù)雜邏輯的產(chǎn)生并 且產(chǎn)生晦澀當(dāng)要使用遞歸鎖的時候應(yīng)該重新審視自己的代碼是否一定要使用遞歸鎖2. 遞歸鎖比起非遞歸鎖效率會低3. 遞歸鎖雖然允許同一個線程多次獲得同一個互斥量但可重復(fù)獲得的最大次數(shù)并未具體說明一旦 超過一定的次數(shù)再對lock進(jìn)行調(diào)用就會拋出std::system錯誤。1.2.3 帶超時的互斥量std::timed_mutex和 std::recursive_timed_mutexstd::timed_mutex比std::mutex多了兩個超時獲取鎖的接口try_lock_for和try_lock_until//1-2-timed_mutex #include iostream #include thread #include mutex #include chrono std::timed_mutex mutex; void work() { std::chrono::milliseconds timeout(100); while (true) { if (mutex.try_lock_for(timeout)) { std::cout std::this_thread::get_id() : do work with the mutex std::endl; std::chrono::milliseconds sleepDuration(250); std::this_thread::sleep_for(sleepDuration); mutex.unlock(); std::this_thread::sleep_for(sleepDuration); } else { std::cout std::this_thread::get_id() : do work without the mutex std::endl; std::chrono::milliseconds sleepDuration(100); std::this_thread::sleep_for(sleepDuration); } } } int main(void) { std::thread t1(work); std::thread t2(work); t1.join(); t2.join(); std::cout main finish ; return 0; }1.2.4 lock_guard和unique_lock的使用和區(qū)別相對于手動lock和unlock我們可以使用RAII(通過類的構(gòu)造析構(gòu))來實現(xiàn)更好的編碼方式。RAII也稱為“資源獲取就是初始化”是c等編程語言常用的管理資源、避免內(nèi)存泄露的方法。它保證 在任何情況下使用對象時先構(gòu)造對象最后析構(gòu)對象。1 unique_lock,lock_guard的使用這里涉及到unique_lock,lock_guard的使用。范例1-2-4-lock#include iostream // std::cout #include thread // std::thread #include mutex // std::mutex, std::lock_guard #include stdexcept // std::logic_error std::mutex mtx; void print_even (int x) { if (x%20) std::cout x is even ; else throw (std::logic_error(not even)); } void print_thread_id (int id) { try { // 這里的lock_guard換成unique_lock是一樣的。 // using a local lock_guard to lock mtx guarantees unlocking on destruction / exception: // std::lock_guardstd::mutex lck (mtx); std::unique_lockstd::mutex lck (mtx); print_even(id); } catch (std::logic_error) { std::cout [exception caught] ; } } int main () { std::thread threads[10]; // spawn 10 threads: for (int i0; i10; i) threads[i] std::thread(print_thread_id,i1); for (auto th : threads) th.join(); return 0; }這里的lock_guard換成unique_lock是一樣的。2 unique_lock,lock_guard的區(qū)別unique_lock與lock_guard都能實現(xiàn)自動加鎖和解鎖但是前者更加靈活能實現(xiàn)更多的功能。unique_lock可以進(jìn)行臨時解鎖和再上鎖如在構(gòu)造對象之后使用lck.unlock()就可以進(jìn)行解鎖 lck.lock()進(jìn)行上鎖而不必等到析構(gòu)時自動解鎖。#include iostream #include deque #include thread #include mutex #include condition_variable #include unistd.h std::dequeint q; std::mutex mu; std::condition_variable cond; int count 0; void fun1() { while (true) { { std::unique_lockstd::mutex locker(mu); // 能否換成lock_guard lock std::cout fun1 lock ; q.push_front(count); // locker.unlock(); // 這里是不是必須的 lock_guard是沒有手動釋放鎖的 unlock cond.notify_one(); //condition_variable 條件和Linux條件變量一樣的 } sleep(1); } } void fun2() { while (true) { std::unique_lockstd::mutex locker(mu); std::cout fun2 lock ; std::cout fun2 wait into ; cond.wait(locker, [](){return !q.empty();}); std::cout fun2 wait leave ; auto data q.back(); q.pop_back(); // locker.unlock(); // 這里是不是必須的 std::cout thread2 get value form thread1: data std::endl; } } int main() { std::thread t1(fun1); std::thread t2(fun2); t1.join(); t2.join(); return 0; }條件變量的目的就是為了在沒有獲得某種提醒時長時間休眠; 如果正常情況下, 我們需要一直循環(huán) (sleep), 這樣的問題就是CPU消耗時延問題條件變量的意思是在cond.wait這里一直休眠直到 cond.notify_one喚醒才開始執(zhí)行下一句; 還有cond.notify_all()接口用于喚醒所有等待的線程。那么為什么必須使用unique_lock呢?原因: 條件變量在wait時會進(jìn)行unlock再進(jìn)入休眠, lock_guard并無該操作接口wait:如果線程被喚醒或者超時那么會先進(jìn)行l(wèi)ock獲取鎖, 再判斷條件(傳入的參數(shù))是否成立, 如果成立則 wait函數(shù)返回否則釋放鎖繼續(xù)休眠notify:進(jìn)行notify動作并不需要獲取鎖使用場景需要結(jié)合notifywait的場景使用unique_lock 如果只是單純的互斥使用lock_guard3 總結(jié)lock_guard1.std::lock_guard 在構(gòu)造函數(shù)中進(jìn)行加鎖析構(gòu)函數(shù)中進(jìn)行解鎖。2.鎖在多線程編程中使用較多因此c11提供了lock_guard模板類在實際編程中我們也可以根 據(jù)自己的場景編寫resource_guard RAII類避免忘掉釋放資源。std::unique_lock1. unique_lock 是通用互斥包裝器允許延遲鎖定、鎖定的有時限嘗試、遞歸鎖定、所有權(quán)轉(zhuǎn)移和與 條件變量一同使用。2. unique_lock比lock_guard使用更加靈活功能更加強大。3. 使用unique_lock需要付出更多的時間、性能成本。參考鏈接0voice · GitHub
版權(quán)聲明: 本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請聯(lián)系我們進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

投資公司網(wǎng)站建設(shè)方案長沙網(wǎng)站開發(fā)設(shè)計

投資公司網(wǎng)站建設(shè)方案,長沙網(wǎng)站開發(fā)設(shè)計,離婚協(xié)議書正規(guī)模板,商場vi設(shè)計 知名公司《構(gòu)網(wǎng)型變流器系統(tǒng)與控制》完整專業(yè)化課程目錄 本課程旨在系統(tǒng)性地構(gòu)建從物理本質(zhì)到工程應(yīng)用的構(gòu)網(wǎng)型變流器完整知識體系#

2026/01/23 11:51:01

最權(quán)威的排行榜網(wǎng)站網(wǎng)站域名繳費

最權(quán)威的排行榜網(wǎng)站,網(wǎng)站域名繳費,網(wǎng)絡(luò)營銷課程設(shè)計心得體會,昆山建設(shè)工程招標(biāo)網(wǎng)站如何通過Anything-LLM優(yōu)化大模型Token利用率#xff1f; 在當(dāng)前大模型應(yīng)用迅速落地的浪潮中#xff0c;

2026/01/21 13:00:01

溫州網(wǎng)站設(shè)計圖片大全二次開發(fā)平臺

溫州網(wǎng)站設(shè)計圖片大全,二次開發(fā)平臺,網(wǎng)站設(shè)計與網(wǎng)頁制作模板,早那么做商城網(wǎng)站Redis跨地域部署終極方案#xff1a;Codis兩地三中心架構(gòu)深度解析 【免費下載鏈接】codis 項目地址: h

2026/01/23 08:02:01