成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

C++11中怎么使用之Lock實現(xiàn)并發(fā)-創(chuàng)新互聯(lián)

C++11中怎么使用之Lock實現(xiàn)并發(fā),很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

站在用戶的角度思考問題,與客戶深入溝通,找到祁門網(wǎng)站設(shè)計與祁門網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、空間域名、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋祁門地區(qū)。

C++11 標準為我們提供了兩種基本的鎖類型,分別如下:

  • std::lock_guard,與 Mutex RAII 相關(guān),方便線程對互斥量上鎖。

  • std::unique_lock,與 Mutex RAII 相關(guān),方便線程對互斥量上鎖,但提供了更好的上鎖和解鎖控制。

另外還提供了幾個與鎖類型相關(guān)的 Tag 類,分別如下:

  • std::adopt_lock_t,一個空的標記類,定義如下:

struct adopt_lock_t {};

該類型的常量對象adopt_lock(adopt_lock 是一個常量對象,定義如下:

constexpr adopt_lock_t adopt_lock {};,// constexpr 是 C++11 中的新關(guān)鍵字)

通常作為參數(shù)傳入給 unique_lock 或 lock_guard 的構(gòu)造函數(shù)。

std::defer_lock_t,一個空的標記類,定義如下:

struct defer_lock_t {};

該類型的常量對象 defer_lock(defer_lock 是一個常量對象,定義如下:

constexpr defer_lock_t defer_lock {};,// constexpr 是 C++11 中的新關(guān)鍵字)

通常作為參數(shù)傳入給 unique_lock 或 lock_guard 的構(gòu)造函數(shù)。

std::try_to_lock_t,一個空的標記類,定義如下:

struct try_to_lock_t {};

該類型的常量對象 try_to_lock(try_to_lock 是一個常量對象,定義如下:

constexpr try_to_lock_t try_to_lock {};,// constexpr 是 C++11 中的新關(guān)鍵字)

通常作為參數(shù)傳入給 unique_lock 或 lock_guard 的構(gòu)造函數(shù)。后面我們會詳細介紹以上三種 Tag 類型在配合 lock_gurad 與 unique_lock 使用時的區(qū)別。

std::lock_guard 介紹

std::lock_gurad 是 C++11 中定義的模板類。定義如下:

template <class Mutex> class lock_guard;

lock_guard 對象通常用于管理某個鎖(Lock)對象,因此與 Mutex RAII 相關(guān),方便線程對互斥量上鎖,即在某個 lock_guard 對象的聲明周期內(nèi),它所管理的鎖對象會一直保持上鎖狀態(tài);而 lock_guard 的生命周期結(jié)束之后,它所管理的鎖對象會被解鎖(注:類似 shared_ptr 等智能指針管理動態(tài)分配的內(nèi)存資源 )。

模板參數(shù) Mutex 代表互斥量類型,例如 std::mutex 類型,它應(yīng)該是一個基本的 BasicLockable 類型,標準庫中定義幾種基本的 BasicLockable 類型,分別 std::mutex, std::recursive_mutex, std::timed_mutex,std::recursive_timed_mutex (以上四種類型均已在上一篇博客中介紹)以及 std::unique_lock(本文后續(xù)會介紹 std::unique_lock)。(注:BasicLockable 類型的對象只需滿足兩種操作,lock 和 unlock,另外還有 Lockable 類型,在 BasicLockable 類型的基礎(chǔ)上新增了 try_lock 操作,因此一個滿足 Lockable 的對象應(yīng)支持三種操作:lock,unlock 和 try_lock;最后還有一種 TimedLockable 對象,在 Lockable 類型的基礎(chǔ)上又新增了 try_lock_for 和 try_lock_until 兩種操作,因此一個滿足 TimedLockable 的對象應(yīng)支持五種操作:lock, unlock, try_lock, try_lock_for, try_lock_until)。

在 lock_guard 對象構(gòu)造時,傳入的 Mutex 對象(即它所管理的 Mutex 對象)會被當前線程鎖住。在lock_guard 對象被析構(gòu)時,它所管理的 Mutex 對象會自動解鎖,由于不需要程序員手動調(diào)用 lock 和 unlock 對 Mutex 進行上鎖和解鎖操作,因此這也是最簡單安全的上鎖和解鎖方式,尤其是在程序拋出異常后先前已被上鎖的 Mutex 對象可以正確進行解鎖操作,極大地簡化了程序員編寫與 Mutex 相關(guān)的異常處理代碼。

值得注意的是,lock_guard 對象并不負責管理 Mutex 對象的生命周期,lock_guard 對象只是簡化了 Mutex 對象的上鎖和解鎖操作,方便線程對互斥量上鎖,即在某個 lock_guard 對象的聲明周期內(nèi),它所管理的鎖對象會一直保持上鎖狀態(tài);而 lock_guard 的生命周期結(jié)束之后,它所管理的鎖對象會被解鎖。

std::lock_guard 構(gòu)造函數(shù)

lock_guard 構(gòu)造函數(shù)如下表所示:

locking (1)
explicit lock_guard (mutex_type& m);
 
adopting (2)
lock_guard (mutex_type& m, adopt_lock_t tag);
 
copy [deleted](3)
lock_guard (const lock_guard&) = delete;
 

locking 初始化
lock_guard 對象管理 Mutex 對象 m,并在構(gòu)造時對 m 進行上鎖(調(diào)用 m.lock())。

adopting初始化
lock_guard 對象管理 Mutex 對象 m,與 locking 初始化(1) 不同的是, Mutex 對象 m 已被當前線程鎖住。

拷貝構(gòu)造
lock_guard 對象的拷貝構(gòu)造和移動構(gòu)造(move construction)均被禁用,因此 lock_guard 對象不可被拷貝構(gòu)造或移動構(gòu)造。

我們來看一個簡單的例子(參考):

#include <iostream>    // std::cout
#include <thread>     // std::thread
#include <mutex>     // std::mutex, std::lock_guard, std::adopt_lock

std::mutex mtx;      // mutex for critical section

void print_thread_id (int id) {
 mtx.lock();
 std::lock_guard<std::mutex> lck(mtx, std::adopt_lock);
 std::cout << "thread #" << id << '\n';
}

int main ()
{
 std::thread threads[10];
 // spawn 10 threads:
 for (int i=0; i<10; ++i)
  threads[i] = std::thread(print_thread_id,i+1);

 for (auto& th : threads) th.join();

 return 0;
}

在 print_thread_id 中,我們首先對 mtx 進行上鎖操作(mtx.lock();),然后用 mtx 對象構(gòu)造一個 lock_guard 對象(std::lock_guard<std::mutex> lck(mtx, std::adopt_lock);),注意此時 Tag 參數(shù)為 std::adopt_lock,表明當前線程已經(jīng)獲得了鎖,此后 mtx 對象的解鎖操作交由 lock_guard 對象 lck 來管理,在 lck 的生命周期結(jié)束之后,mtx 對象會自動解鎖。lock_guard 大的特點就是安全易于使用,請看下面例子(參考),在異常拋出的時候通過 lock_guard 對象管理的 Mutex 可以得到正確地解鎖。

#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%2==0) std::cout << x << " is even\n";
 else throw (std::logic_error("not even"));
}

void print_thread_id (int id) {
 try {
  // using a local lock_guard to lock mtx guarantees unlocking on destruction / exception:
  std::lock_guard<std::mutex> lck (mtx);
  print_even(id);
 }
 catch (std::logic_error&) {
  std::cout << "[exception caught]\n";
 }
}

int main ()
{
 std::thread threads[10];
 // spawn 10 threads:
 for (int i=0; i<10; ++i)
  threads[i] = std::thread(print_thread_id,i+1);

 for (auto& th : threads) th.join();

 return 0;
}

std::unique_lock 介紹

但是 lock_guard 大的缺點也是簡單,沒有給程序員提供足夠的靈活度,因此,C++11 標準中定義了另外一個與 Mutex RAII 相關(guān)類 unique_lock,該類與 lock_guard 類相似,也很方便線程對互斥量上鎖,但它提供了更好的上鎖和解鎖控制。

顧名思義,unique_lock 對象以獨占所有權(quán)的方式( unique owership)管理 mutex 對象的上鎖和解鎖操作,所謂獨占所有權(quán),就是沒有其他的 unique_lock 對象同時擁有某個 mutex 對象的所有權(quán)。

在構(gòu)造(或移動(move)賦值)時,unique_lock 對象需要傳遞一個 Mutex 對象作為它的參數(shù),新創(chuàng)建的 unique_lock 對象負責傳入的 Mutex 對象的上鎖和解鎖操作。

std::unique_lock 對象也能保證在其自身析構(gòu)時它所管理的 Mutex 對象能夠被正確地解鎖(即使沒有顯式地調(diào)用 unlock 函數(shù))。因此,和 lock_guard 一樣,這也是一種簡單而又安全的上鎖和解鎖方式,尤其是在程序拋出異常后先前已被上鎖的 Mutex 對象可以正確進行解鎖操作,極大地簡化了程序員編寫與 Mutex 相關(guān)的異常處理代碼。

值得注意的是,unique_lock 對象同樣也不負責管理 Mutex 對象的生命周期,unique_lock 對象只是簡化了 Mutex 對象的上鎖和解鎖操作,方便線程對互斥量上鎖,即在某個 unique_lock 對象的聲明周期內(nèi),它所管理的鎖對象會一直保持上鎖狀態(tài);而 unique_lock 的生命周期結(jié)束之后,它所管理的鎖對象會被解鎖,這一點和 lock_guard 類似,但 unique_lock 給程序員提供了更多的自由,我會在下面的內(nèi)容中給大家介紹 unique_lock 的用法。

另外,與 lock_guard 一樣,模板參數(shù) Mutex 代表互斥量類型,例如 std::mutex 類型,它應(yīng)該是一個基本的 BasicLockable 類型,標準庫中定義幾種基本的 BasicLockable 類型,分別 std::mutex, std::recursive_mutex, std::timed_mutex,std::recursive_timed_mutex (以上四種類型均已在上一篇博客中介紹)以及 std::unique_lock(本文后續(xù)會介紹 std::unique_lock)。(注:BasicLockable 類型的對象只需滿足兩種操作,lock 和 unlock,另外還有 Lockable 類型,在 BasicLockable 類型的基礎(chǔ)上新增了 try_lock 操作,因此一個滿足 Lockable 的對象應(yīng)支持三種操作:lock,unlock 和 try_lock;最后還有一種 TimedLockable 對象,在 Lockable 類型的基礎(chǔ)上又新增了 try_lock_for 和 try_lock_until 兩種操作,因此一個滿足 TimedLockable 的對象應(yīng)支持五種操作:lock, unlock, try_lock, try_lock_for, try_lock_until)。

std::unique_lock 構(gòu)造函數(shù)

std::unique_lock 的構(gòu)造函數(shù)的數(shù)目相對來說比 std::lock_guard 多,其中一方面也是因為 std::unique_lock 更加靈活,從而在構(gòu)造 std::unique_lock 對象時可以接受額外的參數(shù)??偟貋碚f,std::unique_lock 構(gòu)造函數(shù)如下:

default (1)
unique_lock() noexcept;
 
locking (2)
explicit unique_lock(mutex_type& m);
 
try-locking (3)
unique_lock(mutex_type& m, try_to_lock_t tag);
 
deferred (4)
unique_lock(mutex_type& m, defer_lock_t tag) noexcept;
 
adopting (5)
unique_lock(mutex_type& m, adopt_lock_t tag);
 
locking for (6)
template <class Rep, class Period>
unique_lock(mutex_type& m, const chrono::duration<Rep,Period>& rel_time);
 
locking until (7)
template <class Clock, class Duration>
unique_lock(mutex_type& m, const chrono::time_point<Clock,Duration>& abs_time);
 
copy [deleted] (8)
unique_lock(const unique_lock&) = delete;
 
move (9)
unique_lock(unique_lock&& x);
 

下面我們來分別介紹以上各個構(gòu)造函數(shù):

(1) 默認構(gòu)造函數(shù)
新創(chuàng)建的 unique_lock 對象不管理任何 Mutex 對象。
(2) locking 初始化
新創(chuàng)建的 unique_lock 對象管理 Mutex 對象 m,并嘗試調(diào)用 m.lock() 對 Mutex 對象進行上鎖,如果此時另外某個 unique_lock 對象已經(jīng)管理了該 Mutex 對象 m,則當前線程將會被阻塞。
(3) try-locking 初始化
新創(chuàng)建的 unique_lock 對象管理 Mutex 對象 m,并嘗試調(diào)用 m.try_lock() 對 Mutex 對象進行上鎖,但如果上鎖不成功,并不會阻塞當前線程。
(4) deferred 初始化
新創(chuàng)建的 unique_lock 對象管理 Mutex 對象 m,但是在初始化的時候并不鎖住 Mutex 對象。 m 應(yīng)該是一個沒有當前線程鎖住的 Mutex 對象。
(5) adopting 初始化
新創(chuàng)建的 unique_lock 對象管理 Mutex 對象 m, m 應(yīng)該是一個已經(jīng)被當前線程鎖住的 Mutex 對象。(并且當前新創(chuàng)建的 unique_lock 對象擁有對鎖(Lock)的所有權(quán))。
(6) locking 一段時間(duration)
新創(chuàng)建的 unique_lock 對象管理 Mutex 對象 m,并試圖通過調(diào)用 m.try_lock_for(rel_time) 來鎖住 Mutex 對象一段時間(rel_time)。
(7) locking 直到某個時間點(time point)
新創(chuàng)建的 unique_lock 對象管理 Mutex 對象m,并試圖通過調(diào)用 m.try_lock_until(abs_time) 來在某個時間點(abs_time)之前鎖住 Mutex 對象。
(8) 拷貝構(gòu)造 [被禁用]
unique_lock 對象不能被拷貝構(gòu)造。
(9) 移動(move)構(gòu)造
新創(chuàng)建的 unique_lock 對象獲得了由 x 所管理的 Mutex 對象的所有權(quán)(包括當前 Mutex 的狀態(tài))。調(diào)用 move 構(gòu)造之后, x 對象如同通過默認構(gòu)造函數(shù)所創(chuàng)建的,就不再管理任何 Mutex 對象了。
綜上所述,由 (2) 和 (5) 創(chuàng)建的 unique_lock 對象通常擁有 Mutex 對象的鎖。而通過 (1) 和 (4) 創(chuàng)建的則不會擁有鎖。通過 (3),(6) 和 (7) 創(chuàng)建的 unique_lock 對象,則在 lock 成功時獲得鎖。

關(guān)于unique_lock 的構(gòu)造函數(shù),請看下面例子(參考):

#include <iostream>    // std::cout
#include <thread>     // std::thread
#include <mutex>     // std::mutex, std::lock, std::unique_lock
             // std::adopt_lock, std::defer_lock
std::mutex foo,bar;

void task_a () {
 std::lock (foo,bar);     // simultaneous lock (prevents deadlock)
 std::unique_lock<std::mutex> lck1 (foo,std::adopt_lock);
 std::unique_lock<std::mutex> lck2 (bar,std::adopt_lock);
 std::cout << "task a\n";
 // (unlocked automatically on destruction of lck1 and lck2)
}

void task_b () {
 // foo.lock(); bar.lock(); // replaced by:
 std::unique_lock<std::mutex> lck1, lck2;
 lck1 = std::unique_lock<std::mutex>(bar,std::defer_lock);
 lck2 = std::unique_lock<std::mutex>(foo,std::defer_lock);
 std::lock (lck1,lck2);    // simultaneous lock (prevents deadlock)
 std::cout << "task b\n";
 // (unlocked automatically on destruction of lck1 and lck2)
}


int main ()
{
 std::thread th2 (task_a);
 std::thread th3 (task_b);

 th2.join();
 th3.join();

 return 0;
}

std::unique_lock 移動(move assign)賦值操作

std::unique_lock 支持移動賦值(move assignment),但是普通的賦值被禁用了,

move (1)
unique_lock& operator= (unique_lock&& x) noexcept;
 
copy [deleted] (2)
unique_lock& operator= (const unique_lock&) = delete;
 

移動賦值(move assignment)之后,由 x 所管理的 Mutex 對象及其狀態(tài)將會被新的 std::unique_lock 對象取代。

如果被賦值的對象之前已經(jīng)獲得了它所管理的 Mutex 對象的鎖,則在移動賦值(move assignment)之前會調(diào)用 unlock 函數(shù)釋放它所占有的鎖。

調(diào)用移動賦值(move assignment)之后, x 對象如同通過默認構(gòu)造函數(shù)所創(chuàng)建的,也就不再管理任何 Mutex 對象了。請看下面例子(參考):

#include <iostream>    // std::cout
#include <thread>     // std::thread
#include <mutex>     // std::mutex, std::unique_lock

std::mutex mtx;      // mutex for critical section

void print_fifty (char c) {
 std::unique_lock<std::mutex> lck;     // default-constructed
 lck = std::unique_lock<std::mutex>(mtx); // move-assigned
 for (int i=0; i<50; ++i) { std::cout << c; }
 std::cout << '\n';
}

int main ()
{
 std::thread th2 (print_fifty,'*');
 std::thread th3 (print_fifty,'$');

 th2.join();
 th3.join();

 return 0;
}

std::unique_lock 主要成員函數(shù)

本節(jié)我們來看看 std::unique_lock 的主要成員函數(shù)。由于 std::unique_lock 比 std::lock_guard 操作靈活,因此它提供了更多成員函數(shù)。具體分類如下:

  1. 上鎖/解鎖操作:lock,try_lock,try_lock_for,try_lock_until 和 unlock

  2. 修改操作:移動賦值(move assignment)(前面已經(jīng)介紹過了),交換(swap)(與另一個 std::unique_lock 對象交換它們所管理的 Mutex 對象的所有權(quán)),釋放(release)(返回指向它所管理的 Mutex 對象的指針,并釋放所有權(quán))

  3. 獲取屬性操作:owns_lock(返回當前 std::unique_lock 對象是否獲得了鎖)、operator bool()(與 owns_lock 功能相同,返回當前 std::unique_lock 對象是否獲得了鎖)、mutex(返回當前 std::unique_lock 對象所管理的 Mutex 對象的指針)。

std::unique_lock::lock請看下面例子(參考):

上鎖操作,調(diào)用它所管理的 Mutex 對象的 lock 函數(shù)。如果在調(diào)用  Mutex 對象的 lock 函數(shù)時該 Mutex 對象已被另一線程鎖住,則當前線程會被阻塞,直到它獲得了鎖。

該函數(shù)返回時,當前的 unique_lock 對象便擁有了它所管理的 Mutex 對象的鎖。如果上鎖操作失敗,則拋出 system_error 異常。

// unique_lock::lock/unlock
#include <iostream>    // std::cout
#include <thread>     // std::thread
#include <mutex>     // std::mutex, std::unique_lock, std::defer_lock

std::mutex mtx;      // mutex for critical section

void print_thread_id (int id) {
 std::unique_lock<std::mutex> lck (mtx,std::defer_lock);
 // critical section (exclusive access to std::cout signaled by locking lck):
 lck.lock();
 std::cout << "thread #" << id << '\n';
 lck.unlock();
}

int main ()
{
 std::thread threads[10];
 // spawn 10 threads:
 for (int i=0; i<10; ++i)
  threads[i] = std::thread(print_thread_id,i+1);

 for (auto& th : threads) th.join();

 return 0;
}

std::unique_lock::try_lock

上鎖操作,調(diào)用它所管理的 Mutex 對象的 try_lock 函數(shù),如果上鎖成功,則返回 true,否則返回 false。

請看下面例子(參考):

#include <iostream>    // std::cout
#include <vector>     // std::vector
#include <thread>     // std::thread
#include <mutex>     // std::mutex, std::unique_lock, std::defer_lock

std::mutex mtx;      // mutex for critical section

void print_star () {
 std::unique_lock<std::mutex> lck(mtx,std::defer_lock);
 // print '*' if successfully locked, 'x' otherwise: 
 if (lck.try_lock())
  std::cout << '*';
 else          
  std::cout << 'x';
}

int main ()
{
 std::vector<std::thread> threads;
 for (int i=0; i<500; ++i)
  threads.emplace_back(print_star);

 for (auto& x: threads) x.join();

 return 0;
}

std::unique_lock::try_lock_for

上鎖操作,調(diào)用它所管理的 Mutex 對象的 try_lock_for 函數(shù),如果上鎖成功,則返回 true,否則返回 false。

請看下面例子(參考):

#include <iostream>    // std::cout
#include <chrono>     // std::chrono::milliseconds
#include <thread>     // std::thread
#include <mutex>     // std::timed_mutex, std::unique_lock, std::defer_lock

std::timed_mutex mtx;

void fireworks () {
 std::unique_lock<std::timed_mutex> lck(mtx,std::defer_lock);
 // waiting to get a lock: each thread prints "-" every 200ms:
 while (!lck.try_lock_for(std::chrono::milliseconds(200))) {
  std::cout << "-";
 }
 // got a lock! - wait for 1s, then this thread prints "*"
 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
 std::cout << "*\n";
}

int main ()
{
 std::thread threads[10];
 // spawn 10 threads:
 for (int i=0; i<10; ++i)
  threads[i] = std::thread(fireworks);

 for (auto& th : threads) th.join();

 return 0;
}

std::unique_lock::try_lock_until

上鎖操作,調(diào)用它所管理的 Mutex 對象的 try_lock_for 函數(shù),如果上鎖成功,則返回 true,否則返回 false。

請看下面例子(參考):

#include <iostream>    // std::cout
#include <chrono>     // std::chrono::milliseconds
#include <thread>     // std::thread
#include <mutex>     // std::timed_mutex, std::unique_lock, std::defer_lock

std::timed_mutex mtx;

void fireworks () {
 std::unique_lock<std::timed_mutex> lck(mtx,std::defer_lock);
 // waiting to get a lock: each thread prints "-" every 200ms:
 while (!lck.try_lock_for(std::chrono::milliseconds(200))) {
  std::cout << "-";
 }
 // got a lock! - wait for 1s, then this thread prints "*"
 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
 std::cout << "*\n";
}

int main ()
{
 std::thread threads[10];
 // spawn 10 threads:
 for (int i=0; i<10; ++i)
  threads[i] = std::thread(fireworks);

 for (auto& th : threads) th.join();

 return 0;
}

std::unique_lock::unlock

解鎖操作,調(diào)用它所管理的 Mutex 對象的 unlock 函數(shù)。

請看下面例子(參考):

#include <iostream>    // std::cout
#include <thread>     // std::thread
#include <mutex>     // std::mutex, std::unique_lock, std::defer_lock

std::mutex mtx;      // mutex for critical section

void print_thread_id (int id) {
 std::unique_lock<std::mutex> lck (mtx,std::defer_lock);
 // critical section (exclusive access to std::cout signaled by locking lck):
 lck.lock();
 std::cout << "thread #" << id << '\n';
 lck.unlock();
}

int main ()
{
 std::thread threads[10];
 // spawn 10 threads:
 for (int i=0; i<10; ++i)
  threads[i] = std::thread(print_thread_id,i+1);

 for (auto& th : threads) th.join();

 return 0;
}

std::unique_lock::release

返回指向它所管理的 Mutex 對象的指針,并釋放所有權(quán)。

請看下面例子(參考):

#include <iostream>    // std::cout
#include <vector>     // std::vector
#include <thread>     // std::thread
#include <mutex>     // std::mutex, std::unique_lock

std::mutex mtx;
int count = 0;

void print_count_and_unlock (std::mutex* p_mtx) {
 std::cout << "count: " << count << '\n';
 p_mtx->unlock();
}

void task() {
 std::unique_lock<std::mutex> lck(mtx);
 ++count;
 print_count_and_unlock(lck.release());
}

int main ()
{
 std::vector<std::thread> threads;
 for (int i=0; i<10; ++i)
  threads.emplace_back(task);

 for (auto& x: threads) x.join();

 return 0;
}

std::unique_lock::owns_lock

返回當前 std::unique_lock 對象是否獲得了鎖。

請看下面例子(參考):

#include <iostream>    // std::cout
#include <vector>     // std::vector
#include <thread>     // std::thread
#include <mutex>     // std::mutex, std::unique_lock, std::try_to_lock

std::mutex mtx;      // mutex for critical section

void print_star () {
 std::unique_lock<std::mutex> lck(mtx,std::try_to_lock);
 // print '*' if successfully locked, 'x' otherwise: 
 if (lck.owns_lock())
  std::cout << '*';
 else          
  std::cout << 'x';
}

int main ()
{
 std::vector<std::thread> threads;
 for (int i=0; i<500; ++i)
  threads.emplace_back(print_star);

 for (auto& x: threads) x.join();

 return 0;
}

std::unique_lock::operator bool()

與 owns_lock 功能相同,返回當前 std::unique_lock 對象是否獲得了鎖。

請看下面例子(參考):

#include <iostream>    // std::cout
#include <vector>     // std::vector
#include <thread>     // std::thread
#include <mutex>     // std::mutex, std::unique_lock, std::try_to_lock

std::mutex mtx;      // mutex for critical section

void print_star () {
 std::unique_lock<std::mutex> lck(mtx,std::try_to_lock);
 // print '*' if successfully locked, 'x' otherwise: 
 if (lck)
  std::cout << '*';
 else          
  std::cout << 'x';
}

int main ()
{
 std::vector<std::thread> threads;
 for (int i=0; i<500; ++i)
  threads.emplace_back(print_star);

 for (auto& x: threads) x.join();

 return 0;
}

std::unique_lock::mutex

返回當前 std::unique_lock 對象所管理的 Mutex 對象的指針。

請看下面例子(參考):

#include <iostream>    // std::cout
#include <thread>     // std::thread
#include <mutex>     // std::mutex, std::unique_lock, std::defer_lock

class MyMutex : public std::mutex {
 int _id;
public:
 MyMutex (int id) : _id(id) {}
 int id() {return _id;}
};

MyMutex mtx (101);

void print_ids (int id) {
 std::unique_lock<MyMutex> lck (mtx);
 std::cout << "thread #" << id << " locked mutex " << lck.mutex()->id() << '\n';
}

int main ()
{
 std::thread threads[10];
 // spawn 10 threads:
 for (int i=0; i<10; ++i)
  threads[i] = std::thread(print_ids,i+1);

 for (auto& th : threads) th.join();

 return 0;
}

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)建站的支持。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站jinyejixie.com,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、建站服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

文章名稱:C++11中怎么使用之Lock實現(xiàn)并發(fā)-創(chuàng)新互聯(lián)
文章源于:http://jinyejixie.com/article30/gpsso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、Google、標簽優(yōu)化響應(yīng)式網(wǎng)站、網(wǎng)站制作、小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)
双流县| 敖汉旗| 定南县| 板桥市| 新竹县| 六安市| 张家口市| 徐汇区| 吉木乃县| 客服| 瓮安县| 莱西市| 青龙| 宁德市| 贞丰县| 寿阳县| 江永县| 克东县| 五华县| 六盘水市| 绥化市| 来安县| 四会市| 宣汉县| 屏东市| 盐池县| 陕西省| 江达县| 中卫市| 修文县| 溆浦县| 龙岩市| 遂川县| 通河县| 家居| 乌鲁木齐市| 孙吴县| 探索| 韶山市| 双鸭山市| 奇台县|