junlon2006 / cpp-feature

c++11 feature etc.
Apache License 2.0
0 stars 1 forks source link

c++11 recursive_mutex #5

Open junlon2006 opened 4 years ago

junlon2006 commented 4 years ago

尽量不要使用递归锁 1、能用递归锁的多线程互斥往往可以简化,可以优化为非递归,递归锁逻辑晦涩。 2、递归锁比非递归的性能低一些。 3、递归锁虽然允许同一个线程多次获取同一个互斥量,可重复获取的最大次数并未具体说明,一旦超过一定次数,再次lock会抛出std::system异常。

junlon2006 commented 4 years ago

对于无限递归的测试,在macOs下测试出最大值是65536

static void __get_mutex() {
    int count = 0;
    std::recursive_mutex mutex;
    while (1) {
        cout<<count++<<endl;
        mutex.lock();
    }
}

image