zoukankan      html  css  js  c++  java
  • boost::mutex vs boost::recursive_mutex

    boost::mutex is not re-entrant, a thread can only lock it once, otherwise it’s dead-locked. The following code snippet demonstrates it:

    #include "boost/thread/mutex.hpp"

    #include <iostream>

    boost::mutex mtx;

    void bar(){

        boost::mutex::scoped_lock lLock(mtx);//!! dead-locked here

        std::cout << "bar" << std::endl;

    }

    void foo() {

        boost::mutex::scoped_lock lLock(mtx);

        std::cout << "foo" << std::endl;

        bar();

    }

    int _tmain(int argc, _TCHAR* argv[]){

        foo();

        return 0;

    }

    If you need re-entrant mutex, the boost::recursive_mutex is the choice. The following code snippet demonstrates it:

    #include "boost/thread/recursive_mutex.hpp"

    #include <iostream>

    boost::recursive_mutex r_mtx;

    void bar(){

        boost::recursive_mutex::scoped_lock lLock(r_mtx);//no problem for a thread to lock more than once

        std::cout << "bar" << std::endl;

    }

    void foo() {

    boost::recursive_mutex::scoped_lock lLock(r_mtx);

        std::cout << "foo" << std::endl;

        bar();

    }

    int _tmain(int argc, _TCHAR* argv[]){

        foo();

        return 0;

    }

    I also did a benchmark on my PC. For 1 locking operation, the result is approximately: boost::mutex: 0.043 micro second, boost::recursive_mutex: 0.068 micro second.

    Re-entrant mutex is the default in Java and C#. Generally speaking, if a mutex is shared by many modules/classes, it’s recommended to use boost::recursive_mutex; while if it’s only used by a single module/class and no re-entrant feature needed, it’s recommended to use boost::mutex.

  • 相关阅读:
    binutils工具集之---objcopy,ranlib,size,strings,strip
    FreeRtos——多任务
    IntelliJ Idea 常用快捷键
    了解Spring-boot-starter常用依赖模块
    Spring Boot项目的内嵌容器
    Spring Boot 简介
    webstorm的个性化设置settings
    webstorm使用心得
    webstorm快捷键
    WebStorm使用快速入门
  • 原文地址:https://www.cnblogs.com/weidagang2046/p/1916794.html
Copyright © 2011-2022 走看看