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

请看下面例子(参考):

  #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

解锁操作,调用它所管理的 Mutex 对象的 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::release

返回指向它所管理的 Mutex 对象的指针,并释放所有权。

请看下面例子(参考):

  #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 对象的指针。

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

ctvol管理联系方式QQ:251552304

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

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

精彩推荐