C++11 并发指南之Lock 详解分享!

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

移动赋值(move assignment)之后,由 x 所管理的 Mutex 对象及其状态将会被新的 std::unique_lock 对象取代。

如果被赋值的对象之前已经获得了它所管理的 Mutex 对象的锁,则在移动赋值(move assignment)之前会调用 unlock 函数释放它所占有的锁。

调用移动赋值(move assignment)之后, x 对象如同通过默认构造函数所创建的,也就不再管理任何 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 th1 (print_fifty,'*');   std::thread th2 (print_fifty,'$');     th1.join();   th2.join();     return 0;  }

std::unique_lock 主要成员函数

本节我们来看看 std::unique_lock 的主要成员函数。由于 std::unique_lock 比 std::lock_guard 操作灵活,因此它提供了更多成员函数。具体分类如下:

  1. 上锁/解锁操作:lock,try_lock,try_lock_for,try_lock_until 和 unlock
  2. 修改操作:移动赋值(move assignment)(前面已经介绍过了),交换(swap)(与另一个 std::unique_lock 对象交换它们所管理的 Mutex 对象的所有权),释放(release)(返回指向它所管理的 Mutex 对象的指针,并释放所有权)
  3. 获取属性操作:owns_lock(返回当前 std::unique_lock 对象是否获得了锁)、operator bool()(与 owns_lock 功能相同,返回当前 std::unique_lock 对象是否获得了锁)、mutex(返回当前 std::unique_lock 对象所管理的 Mutex 对象的指针)。

std::unique_lock::lock请看下面例子(参考):

上锁操作,调用它所管理的 Mutex 对象的 lock 函数。如果在调用  Mutex 对象的 lock 函数时该 Mutex 对象已被另一线程锁住,则当前线程会被阻塞,直到它获得了锁。

该函数返回时,当前的 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

上锁操作,调用它所管理的 Mutex 对象的 try_lock 函数,如果上锁成功,则返回 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

上锁操作,调用它所管理的 Mutex 对象的 try_lock_for 函数,如果上锁成功,则返回 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

上锁操作,调用它所管理的 Mutex 对象的 try_lock_for 函数,如果上锁成功,则返回 true,否则返回 false。

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/483688.html

(0)
上一篇 2020年11月10日
下一篇 2020年11月10日

精彩推荐