美文网首页
多线程--mutex、原子操作、读写锁

多线程--mutex、原子操作、读写锁

作者: d9e7e8498548 | 来源:发表于2020-11-01 21:39 被阅读0次

    2. 多线程i++(不加锁、mutex、原子操作)

    • 不加锁

      #include <thread>
      #include <windows.h>
      
      using namespace std;
      
      unsigned long int g_cnt = 0;
      
      void FuncA()
      {
          for (int i = 0; i < 10; ++i) {
              g_cnt++;
              cout << "cnt = " << g_cnt << endl;
              // Sleep(10);
          }
      }
      
      void FuncB()
      {
          for (int i = 0; i < 10; ++i) {
              g_cnt++;
              cout << "cnt = " << g_cnt << endl;
              // Sleep(5);
          }
      }
      
      int main()
      {
          thread t1(FuncA);
          thread t2(FuncB);
          // t1.join();
          // t2.join();
          t1.detach();
          t2.detach();
          cout << "cnt = " << g_cnt << endl;
          // Sleep(1000);
          system("pause");
          return 0;
      }
      
    • mutex

      #include <thread>
      #include <windows.h>
      #include <mutex>
      
      using namespace std;
      
      unsigned long int g_cnt = 0;
      mutex mt; // 线程互斥对象
      
      void FuncA()
      {
          for (int i = 0; i < 10; ++i) {
              std::lock_guard<std::mutex> lockGuard(mt);
              // mt.lock();
              g_cnt++;
              cout << "cnt = " << g_cnt << endl;
              // Sleep(10);
              // mt.unlock();
          }
      }
      
      void FuncB()
      {
          for (int i = 0; i < 10; ++i) {
              std::lock_guard<std::mutex> lockGuard(mt);
              // mt.lock();
              g_cnt++;
              cout << "cnt = " << g_cnt << endl;
              // Sleep(5);
              // mt.unlock();
          }
      }
      
      int main()
      {
          thread t1(FuncA);
          thread t2(FuncB);
          // t1.join();
          // t2.join();
          t1.detach();
          t2.detach();
          cout << "cnt = " << g_cnt << endl;
          // Sleep(1000);
          system("pause");
          return 0;
      }
      

      mutex是用来保证线程同步的,防止不同的线程同时操作同一个共享数据。

      但使用lock_guard则相对安全,它是基于作用域的,能够自解锁,当该对象创建时,它会像m.lock()一样获得互斥锁,当生命周期结束时,它会自动析构(unlock),不会因为某个线程异常退出而影响其他线程。

    • 原子操作

      在C++11中,实现了原子操作的数据类型(atomic_bool,atomic_int,atomic_long等等),对于这些原子数据类型的共享资源的访问,无需借助mutex等锁机制,也能够实现对共享资源的正确访问。

    #include <thread>
    #include <windows.h>
    #include <atomic>
    
    using namespace std;
    
    // unsigned long int g_cnt = 0;
    atomic_long g_cnt;
    
    void FuncA()
    {
        for (int i = 0; i < 10; ++i) {
            g_cnt++;
            cout << "cnt = " << g_cnt << endl;
            // Sleep(10);
        }
    }
    
    void FuncB()
    {
        for (int i = 0; i < 10; ++i) {
            g_cnt++;
            cout << "cnt = " << g_cnt << endl;
            // Sleep(5);
        }
    }
    
    int main()
    {
        thread t1(FuncA);
        thread t2(FuncB);
        // t1.join();
        // t2.join();
        t1.detach();
        t2.detach();
        cout << "cnt = " << g_cnt << endl;
        // Sleep(1000);
        system("pause");
        return 0;
    }
    

    参考:

    1. linux的<pthread.h>
    2. C++使用thread类多线程编程
    3. C++ thread用法总结(整理)
    4. C++11中的原子操作(atomic operation)
    5. Linux C++实现多线程同步的四种方式

    3. 实现一个读写锁

    4. 基于面向对象的思想,实现多线程队列类(基类和子类)

    相关文章

      网友评论

          本文标题:多线程--mutex、原子操作、读写锁

          本文链接:https://www.haomeiwen.com/subject/ibiuvktx.html