偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

C++多線程中的互斥鎖

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

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

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

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

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

特點(diǎn):

  • 簡(jiǎn)單易用,適用于大多數(shù)場(chǎng)景。
  • 不能遞歸鎖定,同一線程多次嘗試鎖定會(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í)功能,允許線程在指定時(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允許同一線程多次獲取鎖而不會(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è)線程同時(shí)讀取,但只有一個(gè)線程可以寫入。這在讀多寫少的場(chǎng)景下非常有用。

特點(diǎn):

  • 適用于讀多寫少的場(chǎng)景。
  • 讀操作和寫操作使用不同的鎖定機(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):

  • 適用于讀多寫少且需要超時(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ù)提供了多種類型的互斥鎖,每種鎖都有其特定的用途和特點(diǎn)。選擇合適的互斥鎖類型可以有效提高程序的并發(fā)性能和安全性。

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

2020-08-26 08:59:58

Linux線程互斥鎖

2024-06-28 08:45:58

2023-12-14 15:05:08

volatile代碼C++

2024-10-14 16:25:59

C#線程鎖代碼

2012-05-18 10:36:20

CC++編程

2010-01-18 14:09:58

C++多線程

2010-02-04 10:19:39

C++多線程

2010-02-05 15:30:54

C++多線程測(cè)試

2021-03-24 08:02:58

C語言

2021-02-25 15:58:46

C++線程編程開發(fā)技術(shù)

2021-03-05 07:38:52

C++線程編程開發(fā)技術(shù)

2025-05-06 08:20:00

互斥鎖C++編程

2024-06-24 12:57:09

多線程C++編程語言

2024-10-21 16:59:37

C#編程多線程

2011-04-25 14:42:10

C#lock

2025-02-17 02:00:00

Monitor機(jī)制代碼

2024-11-05 16:29:57

2011-06-14 15:25:28

C++多線程

2025-04-10 01:01:00

2024-02-02 18:29:54

C++線程編程
點(diǎn)贊
收藏

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