您的位置:

c++lock详解

一、c lock作用

c++lock是一种线程同步机制,用于保护共享资源的访问。在多线程环境下,如果多个线程同时访问了同一份数据,就会导致数据不一致的问题。通过使用c++lock,我们可以保证同一时间只有一个线程能够访问共享资源,从而避免了数据不一致的问题。

二、c lock用法

c++lock的用法非常简单。我们只需要在访问共享资源之前,通过lock()函数锁定共享资源,访问完成后再通过unlock()函数释放锁即可。下面是一段示例代码:

#include
#include
   
#include
    

using namespace std;

int g_num = 0; // 共享资源
mutex g_mutex; // 锁对象

void func()
{
    // 锁定共享资源
    g_mutex.lock();
    
    // 访问共享资源
    cout << "Thread ID: " << this_thread::get_id() << " -> " << ++g_num << endl;
    
    // 释放锁
    g_mutex.unlock();
}

int main()
{
    thread t1(func);
    thread t2(func);
    
    t1.join();
    t2.join();
    
    return 0;
}

    
   
  

三、c lock 参数

c++lock有3种不同的锁类型:unique_lock、lock_guard和defer_lock。其中unique_lock是最灵活的一种,支持锁定和解锁多次,而lock_guard则是最简单的一种,只支持一次性锁定。defer_lock则是延迟锁定,即先不锁定,等到需要访问共享资源时再锁定。下面是一段使用defer_lock参数的示例代码:

#include
#include
   
#include
    

using namespace std;

int g_num = 0; // 共享资源
mutex g_mutex; // 锁对象

void func()
{
    // 延迟锁定
    unique_lock
      lock(g_mutex, defer_lock);
    
    // 访问共享资源前先锁定
    lock.lock();
    
    // 访问共享资源
    cout << "Thread ID: " << this_thread::get_id() << " -> " << ++g_num << endl;
    
    // 解锁
    lock.unlock();
}

int main()
{
    thread t1(func);
    thread t2(func);
    
    t1.join();
    t2.join();
    
    return 0;
}

     
    
   
  

四、c lock this

c++lock使用this锁定时,需要注意避免出现多线程死锁的问题。如果在一个函数中多次锁定this指针,就会出现死锁的问题。下面是一段可能导致死锁的示例代码:

#include
#include
   
#include
    

using namespace std;

class Test
{
public:
    Test() : m_num(0) {}
    
    void func()
    {
        // 第一次锁定this指针
        m_mutex.lock();
        
        // 第二次锁定this指针,会导致死锁
        this->m_mutex.lock();
        
        // 访问共享资源
        cout << "Thread ID: " << this_thread::get_id() << " -> " << ++m_num << endl;
        
        // 解锁
        m_mutex.unlock();
    }
    
private:
    int m_num; // 共享资源
    mutex m_mutex; // 锁对象
};

int main()
{
    Test t;
    thread t1(&Test::func, &t);
    thread t2(&Test::func, &t);
    
    t1.join();
    t2.join();
    
    return 0;
}

    
   
  

五、c lock 无效

c++lock可能会无效的一种情况是,不同线程使用了不同的mutex对象。因为不同的mutex对象无法锁定同一个共享资源,所以c++lock就会无效。下面是一段可能导致c++lock无效的示例代码:

#include
#include
   
#include
    

using namespace std;

int g_num = 0; // 共享资源
mutex g_mutex1; // 锁对象1
mutex g_mutex2; // 锁对象2

void func()
{
    // 使用了不同的mutex对象,无法锁定同一份共享资源
    unique_lock
      lock(g_mutex1);
    
    // 访问共享资源
    cout << "Thread ID: " << this_thread::get_id() << " -> " << ++g_num << endl;
    
    // 解锁
    lock.unlock();
}

int main()
{
    thread t1(func);
    thread t2(func);
    
    t1.join();
    t2.join();
    
    return 0;
}

     
    
   
  

六、c lock 参数要求

c++lock对锁对象的要求非常严格,必须保证所有访问共享资源的线程都使用同一份mutex对象。否则就会出现线程竞争的问题,导致数据不一致。下面是一段使用了不同mutex对象的示例代码:

#include
#include
   
#include
    

using namespace std;

int g_num = 0; // 共享资源
mutex g_mutex1; // 锁对象1
mutex g_mutex2; // 锁对象2

void func1()
{
    // 使用了不同的mutex对象,会导致线程竞争问题
    unique_lock
      lock(g_mutex1);
    
    // 访问共享资源
    cout << "Thread ID: " << this_thread::get_id() << " -> " << ++g_num << endl;
    
    // 解锁
    lock.unlock();
}

void func2()
{
    // 使用了不同的mutex对象,会导致线程竞争问题
    unique_lock
       lock(g_mutex2);
    
    // 访问共享资源
    cout << "Thread ID: " << this_thread::get_id() << " -> " << ++g_num << endl;
    
    // 解锁
    lock.unlock();
}

int main()
{
    thread t1(func1);
    thread t2(func2);
    
    t1.join();
    t2.join();
    
    return 0;
}

      
     
    
   
  

七、clock

c++中的clock函数用于测量程序运行的时间。clock函数返回从程序开始运行起,处理器时钟所需的时间(以时钟周期为单位)。下面是一段使用clock函数测量程序运行时间的示例代码:

#include
#include
   

using namespace std;

int main()
{
    clock_t start_time = clock(); // 记录程序开始时间

    // 执行代码段
    for(int i=0; i<10000000; i++);
    
    clock_t end_time = clock(); // 记录程序结束时间
    double total_time = static_cast
    (end_time - start_time) / CLOCKS_PER_SEC; // 计算总时间
    
    cout << "程序运行时间:" << total_time << " 秒" << endl;
    
    return 0;
}

    
   
  

八、cblock

c++中的cblock语法和c++lock非常相似,都用于保护共享资源。cblock语法本质上是一种代码块,可以在代码块前后使用宏定义来自动加锁和解锁。下面是一段使用cblock保护共享资源的示例代码:

#include
#include
   
#include
    

using namespace std;

int g_num = 0; // 共享资源
mutex g_mutex; // 锁对象

#define C_BLOCK(mutex) for(unique_lock
      lock(mutex); true; lock.unlock()) // 宏定义,用于加锁和解锁

void func()
{
    C_BLOCK(g_mutex)
    {
        // 访问共享资源
        cout << "Thread ID: " << this_thread::get_id() << " -> " << ++g_num << endl;
    }
}

int main()
{
    thread t1(func);
    thread t2(func);
    
    t1.join();
    t2.join();
    
    return 0;
}