ProDeveloperTutorial.com

教程和编程解决方案
菜单
  • Shell脚本
  • 系统设计
  • Linux系统编程
  • 4g LTE
  • 编码问题
  • C
  • C ++
  • DSA
  • GIT

C ++ 11功能:C ++多线程第9章:C ++线程中的唯一锁

前开发者教程 2020年2月26日

在上一章中,我们看到了lock_guard。在本章中,我们将学习独特的锁。

唯一锁是可从C ++ 11获得的类。

唯一锁是互斥锁的包装。它将拥有传递给唯一锁的互斥锁。

 

独特的锁具有以下功能:

1.唯一锁可以具有不同的锁定策略。
2.可以使用基于时间的锁定[try_lock_for,try_lock_until]。
3.还提供递归锁定。
4.使用移动转移锁所有权。
5.它还支持条件变量。

 

以下是唯一锁定中可用的一些锁定策略。

1. defer_lock:它不会获得互斥体的所有权。
2. try_to_lock:它将尝试获取互斥锁的所有权而不会阻塞。
3. 采用锁定:将假定调用线程已获得互斥体的所有权。

当你不穿’如果指定锁定策略,它将仅锁定互斥锁。

关于unique_lock要记住的重要一点是,当互斥锁超出范围时,它将自动解锁互斥锁。

现在,借助示例帮助我们了解unique_lock:

示例1:一个简单的unique_lock

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

// for more tutorials 上  C, C ++, DS visit www.ProDeveloperTutorial.com

using namespace std;

std::mutex mtx;

int count_num = 0;


void increment_fun (int thread_num, int val) 
{
   
    unique_lock<mutex> lock(mtx); // locking mtx mutex 

    for (int i = 0; i < val; ++i)
    {
        cout<<"Thread "<<thread_num<<" count = "<<count_num++<<endl;
    }
    //mtx.unlock();// no need to call unlock
}

int main ()
{
    std::thread t1 (increment_fun, 1, 5);
    std::thread t2 (increment_fun, 2, 6);
    
    t1.join();
    t2.join();
    

  return 0;
}

 

输出:

Thread 1 count = 0
Thread 1 count = 1
Thread 1 count = 2
Thread 1 count = 3
Thread 1 count = 4
Thread 2 count = 5
Thread 2 count = 6
Thread 2 count = 7
Thread 2 count = 8
Thread 2 count = 9
Thread 2 count = 10

 

示例2:带有defer_lock标志的unique_lock

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

// for more tutorials 上  C, C ++, DS visit www.ProDeveloperTutorial.com

using namespace std;

std::mutex mtx;

int count_num = 0;


void increment_fun (int thread_num, int val) 
{
    // as we are using defer_lock flag, it will not lock the mutex "mtx"
    unique_lock<mutex> lock(mtx, defer_lock);

    //hence you explicitly need to call the lock 上  the mutex
    lock.lock();

    for (int i = 0; i < val; ++i)
    {
        cout<<"Thread "<<thread_num<<" count = "<<count_num++<<endl;
    }
    //mtx.unlock();// no need to call unlock, as we are using unique_lock
}

int main ()
{
    std::thread t1 (increment_fun, 1, 5);
    std::thread t2 (increment_fun, 2, 6);
    
    t1.join();
    t2.join();
    

  return 0;
}

 

输出:

Thread 1 count = 0
Thread 1 count = 1
Thread 1 count = 2
Thread 1 count = 3
Thread 1 count = 4
Thread 2 count = 5
Thread 2 count = 6
Thread 2 count = 7
Thread 2 count = 8
Thread 2 count = 9
Thread 2 count = 10

 

 

该网站上可用的教程列表:

C编程20+章C ++编程80+章
100多个编码问题数据结构和算法85+章
系统设计20+章Shell脚本编写12章
4g LTE 60+章节最常见的编码问题
5G NR 50+章Linux系统编程20+章

 

 

 

 

分享
电子邮件
鸣叫
领英
Reddit
绊倒
Pinterest的
上一篇文章
下一篇

关于作者

前开发者教程

每天我们都会讨论竞争性编程问题,请加入我们的网站:   电报频道

ProDeveloperTutorial.com

教程和编程解决方案
版权© 2020 ProDeveloperTutorial.com
从以下课程获得热门课程: 教育性的

  1. <noscript id="HK0Hk0K" class="HzEU4aq"><fieldset id="NBvCgIj"><tr id="Hb6fL8U"></tr></fieldset></noscript>

      • <section id="oN5KM44" class="o7pZso7"><strong id="gqJgPAV"></strong></section>
        <article id="LYBbt4J" class="LKBW2ck"><aside class="vz5Slc6"></aside></article>



          <menu id="f0s3GMG" class="fBVRqis"><table id="Kn26QTp" class="K86d23v"><fieldset id="BHwUNzR" class="B2YomPn"></fieldset></table></menu>