成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

C++多線(xiàn)程中的互斥鎖

開(kāi)發(fā)
C++標(biāo)準(zhǔn)庫(kù)提供了多種類(lèi)型的互斥鎖,每種鎖都有其特定的用途和特點(diǎn)。選擇合適的互斥鎖類(lèi)型可以有效提高程序的并發(fā)性能和安全性。

在多線(xiàn)程編程中,互斥鎖(mutex)是確保線(xiàn)程安全、避免數(shù)據(jù)競(jìng)爭(zhēng)的重要工具。C++標(biāo)準(zhǔn)庫(kù)提供了多種互斥鎖,每種都有其特定的應(yīng)用場(chǎng)景和特點(diǎn)。

主要有以下幾種互斥鎖(Mutex):

  • std::mutex:最基本的互斥鎖,用于保護(hù)臨界區(qū),確保同一時(shí)間只有一個(gè)線(xiàn)程可以訪(fǎng)問(wèn)被保護(hù)的資源。
  • std::timed_mutex:支持超時(shí)機(jī)制的互斥鎖,可以嘗試在給定時(shí)間內(nèi)鎖定互斥鎖。如果在指定時(shí)間內(nèi)沒(méi)有成功獲取鎖,則返回失敗。
  • std::recursive_mutex:遞歸互斥鎖,同一線(xiàn)程可以多次獲取鎖而不會(huì)發(fā)生死鎖,通常用于遞歸函數(shù)中。
  • std::recursive_timed_mutex:支持超時(shí)機(jī)制的遞歸互斥鎖,結(jié)合了遞歸鎖和超時(shí)鎖的特性。
  • std::shared_mutex(C++17 引入):允許多個(gè)線(xiàn)程同時(shí)讀取,但只有一個(gè)線(xiàn)程可以寫(xiě)入。適用于讀多寫(xiě)少的場(chǎng)景。
  • std::shared_timed_mutex(C++17 引入):支持超時(shí)機(jī)制的共享互斥鎖,可以在給定時(shí)間內(nèi)嘗試獲取讀鎖或?qū)戞i。

這些是C++標(biāo)準(zhǔn)庫(kù)中提供的幾種主要的互斥鎖類(lèi)型。每種鎖都有其特定的應(yīng)用場(chǎng)景和使用方法,選擇合適的互斥鎖類(lèi)型對(duì)于實(shí)現(xiàn)高效、安全的多線(xiàn)程程序非常重要。

一、基本互斥鎖(std::mutex)

std::mutex是最基本的互斥鎖,主要用于保護(hù)臨界區(qū),確保同一時(shí)間只有一個(gè)線(xiàn)程可以訪(fǎng)問(wèn)共享資源。

特點(diǎn):

  • 簡(jiǎn)單易用,適用于大多數(shù)場(chǎng)景。
  • 不能遞歸鎖定,同一線(xiàn)程多次嘗試鎖定會(huì)導(dǎo)致死鎖。

示例代碼:

#include <iostream>
#include <thread>
#include <mutex>

std::mutex mtx;

void print_thread_id(int id) {
    std::lock_guard<std::mutex> lock(mtx); // 自動(dòng)管理鎖的獲取和釋放
    std::cout << "Thread ID: " << id << std::endl;
}

int main() {
    std::thread t1(print_thread_id, 1);
    std::thread t2(print_thread_id, 2);

    t1.join();
    t2.join();

    return 0;
}

二、帶超時(shí)機(jī)制的互斥鎖(std::timed_mutex)

std::timed_mutex在std::mutex的基礎(chǔ)上增加了超時(shí)功能,允許線(xiàn)程在指定時(shí)間內(nèi)嘗試獲取鎖,如果在超時(shí)時(shí)間內(nèi)未成功獲取鎖,則返回失敗。

特點(diǎn):

  • 適用于需要設(shè)置鎖獲取超時(shí)時(shí)間的場(chǎng)景。
  • 提供try_lock_for和try_lock_until兩種超時(shí)嘗試獲取鎖的方法。

示例代碼:

#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>

std::timed_mutex tmtx;

void try_to_lock(int id) {
    if(tmtx.try_lock_for(std::chrono::milliseconds(100))) {
        std::cout << "Thread " << id << " locked the mutex" << std::endl;
        std::this_thread::sleep_for(std::chrono::milliseconds(200));
        tmtx.unlock();
    } else {
        std::cout << "Thread " << id << " could not lock the mutex" << std::endl;
    }
}

int main() {
    std::thread t1(try_to_lock, 1);
    std::thread t2(try_to_lock, 2);

    t1.join();
    t2.join();

    return 0;
}

三、遞歸互斥鎖(std::recursive_mutex)

std::recursive_mutex允許同一線(xiàn)程多次獲取鎖而不會(huì)發(fā)生死鎖,這對(duì)于遞歸函數(shù)或需要多次鎖定的場(chǎng)景非常有用。

特點(diǎn):

  • 適用于遞歸調(diào)用和需要多次鎖定的場(chǎng)景。
  • 需要注意避免濫用,因?yàn)檫f歸鎖的使用會(huì)增加鎖定次數(shù)的復(fù)雜性。

示例代碼:

#include <iostream>
#include <thread>
#include <mutex>

std::recursive_mutex rmtx;

void recursive_function(int depth) {
    rmtx.lock();
    std::cout << "Depth: " << depth << std::endl;
    if (depth > 0) {
        recursive_function(depth - 1);
    }
    rmtx.unlock();
}

int main() {
    std::thread t(recursive_function, 5);
    t.join();

    return 0;
}

四、帶超時(shí)機(jī)制的遞歸互斥鎖(std::recursive_timed_mutex)

std::recursive_timed_mutex結(jié)合了std::recursive_mutex和std::timed_mutex的特性,支持遞歸鎖定和超時(shí)機(jī)制。

特點(diǎn):

  • 適用于遞歸調(diào)用和需要超時(shí)機(jī)制的場(chǎng)景。
  • 提供超時(shí)嘗試獲取遞歸鎖的方法。

示例代碼:

#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>

std::recursive_timed_mutex rtmmtx;

void try_recursive_lock(int id, int depth) {
    if (rtmmtx.try_lock_for(std::chrono::milliseconds(100))) {
        std::cout << "Thread " << id << " locked at depth " << depth << std::endl;
        std::this_thread::sleep_for(std::chrono::milliseconds(50));
        if (depth > 0) {
            try_recursive_lock(id, depth - 1);
        }
        rtmmtx.unlock();
    } else {
        std::cout << "Thread " << id << " could not lock at depth " << depth << std::endl;
    }
}

int main() {
    std::thread t1(try_recursive_lock, 1, 3);
    std::thread t2(try_recursive_lock, 2, 3);

    t1.join();
    t2.join();

    return 0;
}

五、共享互斥鎖(std::shared_mutex)

std::shared_mutex允許多個(gè)線(xiàn)程同時(shí)讀取,但只有一個(gè)線(xiàn)程可以寫(xiě)入。這在讀多寫(xiě)少的場(chǎng)景下非常有用。

特點(diǎn):

  • 適用于讀多寫(xiě)少的場(chǎng)景。
  • 讀操作和寫(xiě)操作使用不同的鎖定機(jī)制。

示例代碼:


#include <iostream>
#include <thread>
#include <shared_mutex>

std::shared_mutex shmtx;

void read_shared(int id) {
    std::shared_lock<std::shared_mutex> lock(shmtx); // 共享鎖
    std::cout << "Thread " << id << " is reading" << std::endl;
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
}

void write_shared(int id) {
    std::unique_lock<std::shared_mutex> lock(shmtx); // 獨(dú)占鎖
    std::cout << "Thread " << id << " is writing" << std::endl;
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
}

int main() {
    std::thread readers[5], writer(write_shared, 1);

    for (int i = 0; i < 5; ++i) {
        readers[i] = std::thread(read_shared, i + 2);
    }

    writer.join();
    for (auto& reader : readers) {
        reader.join();
    }

    return 0;
}

六、帶超時(shí)機(jī)制的共享互斥鎖(std::shared_timed_mutex)

std::shared_timed_mutex結(jié)合了std::shared_mutex和std::timed_mutex的特性,支持超時(shí)機(jī)制。

特點(diǎn):

  • 適用于讀多寫(xiě)少且需要超時(shí)機(jī)制的場(chǎng)景。
  • 提供超時(shí)嘗試獲取共享鎖的方法。

示例代碼:


#include <iostream>
#include <thread>
#include <shared_mutex>
#include <chrono>

std::shared_timed_mutex shtmmtx;

void try_read_shared(int id) {
    if (shtmmtx.try_lock_shared_for(std::chrono::milliseconds(100))) {
        std::cout << "Thread " << id << " is reading" << std::endl;
        std::this_thread::sleep_for(std::chrono::milliseconds(50));
        shtmmtx.unlock_shared();
    } else {
        std::cout << "Thread " << id << " could not read" << std::endl;
    }
}

void try_write_shared(int id) {
    if (shtmmtx.try_lock_for(std::chrono::milliseconds(100))) {
        std::cout << "Thread " << id << " is writing" << std::endl;
        std::this_thread::sleep_for(std::chrono::milliseconds(50));
        shtmmtx.unlock();
    } else {
        std::cout << "Thread " << id << " could not write" << std::endl;
    }
}

int main() {
    std::thread readers[5], writer(try_write_shared, 1);

    for (int i = 0; i < 5; ++i) {
        readers[i] = std::thread(try_read_shared, i + 2);
    }

    writer.join();
    for (auto& reader : readers) {
        reader.join();
    }

    return 0;
}

總結(jié)

C++標(biāo)準(zhǔn)庫(kù)提供了多種類(lèi)型的互斥鎖,每種鎖都有其特定的用途和特點(diǎn)。選擇合適的互斥鎖類(lèi)型可以有效提高程序的并發(fā)性能和安全性。

責(zé)任編輯:趙寧寧 來(lái)源: AI讓生活更美好
相關(guān)推薦

2020-08-26 08:59:58

Linux線(xiàn)程互斥鎖

2024-06-28 08:45:58

2023-12-14 15:05:08

volatile代碼C++

2024-10-14 16:25:59

C#線(xiàn)程鎖代碼

2012-05-18 10:36:20

CC++編程

2010-01-18 14:09:58

C++多線(xiàn)程

2010-02-04 10:19:39

C++多線(xiàn)程

2010-02-05 15:30:54

C++多線(xiàn)程測(cè)試

2021-03-24 08:02:58

C語(yǔ)言

2021-02-25 15:58:46

C++線(xiàn)程編程開(kāi)發(fā)技術(shù)

2021-03-05 07:38:52

C++線(xiàn)程編程開(kāi)發(fā)技術(shù)

2025-05-06 08:20:00

互斥鎖C++編程

2024-06-24 12:57:09

多線(xiàn)程C++編程語(yǔ)言

2011-04-25 14:42:10

C#lock

2024-10-21 16:59:37

C#編程多線(xiàn)程

2025-02-17 02:00:00

Monitor機(jī)制代碼

2024-11-05 16:29:57

2024-02-02 18:29:54

C++線(xiàn)程編程

2011-06-14 15:25:28

C++多線(xiàn)程

2025-04-10 01:01:00

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 91在线精品播放 | 91在线视频网址 | 99热电影| 成人免费精品视频 | 免费人成在线观看网站 | 五月婷婷激情 | 国产传媒在线观看 | 久久久青草婷婷精品综合日韩 | 精品国产网 | 亚洲一区久久 | 一区二区三区成人 | 久久婷婷国产香蕉 | 香蕉婷婷 | 久久久久国产成人精品亚洲午夜 | 伊人春色在线 | 日韩av在线一区 | 手机看片1 | 91一区二区| 97久久精品午夜一区二区 | 天天干天天爱天天爽 | 亚洲高清在线 | 日本爱爱视频 | 国产精品一二三区 | 99免费在线视频 | 午夜影院 | 欧美日韩一二三区 | 午夜久草 | 国产小视频精品 | 亚州毛片 | 亚洲精选一区二区 | 国产欧美性成人精品午夜 | 午夜寂寞影院列表 | 国产成人免费 | 精品久久国产老人久久综合 | 欧美激情亚洲天堂 | 麻豆久久| 天天操夜夜骑 | 国产成人福利在线观看 | 精品国产鲁一鲁一区二区张丽 | 伦理午夜电影免费观看 | 黄色片免费在线观看 |