C++ 多线程

一、线程 thread

  • 默认构造函数。
    // 创建一个空的 thread 执行对象
    thread() {
         
      
    }
    
  • 创建 thread 执行对象,该 thread 对象可被 joinable,新产生的线程会调用 fn 函数,该函数的参数由 args 给出。
    template<class Fn, class... Args>
    explicit thread(Fn&& fn, Args&&... args);
    
  • 拷贝构造函数(被禁用),意味着 thread 不可被拷贝构造。
    thread(const thread&) = delete;
    
    thread t1;
    thread t2 = t1; // 错误
    
  • move 构造函数
    • move 构造函数调用成功之后 x 不代表任何 thread 执行对象。
    • 注意:可被 joinablethread 对象必须在他们被销毁之前被主线程 join 或将其设置为 detached
      thread(thread&& x)noexcept
      
      thread t1;
      thread t2 = std::move(t1); // 正确
      
      #include <iostream>
      #include <thread>
      
      using namespace std;
      
      void ThreadFunc(int &a)
      {
              // 引用传递
          cout << "this is thread func" << endl;
          cout << "a = " << (a += 10) << endl; // 20
      }
      
      int main()
      {
             
          int x = 10;
      
          thread t1(ThreadFunc, std::ref(x));
          thread t2(std::move(t1)); // t1 线程失去所有权
      
          thread t3;
          t3 = std::move(t2); // t2 线程失去所有权
          t3.join();
      
          cout << "Main End" << " x = " << x << endl; // 20
      
          return 0;
      }
      
  • 主要成员函数。
    • get_id() ,获取线程 ID,返回类型 std::thread::id 对象。
      std::thread::id main_thread_id = std::this_thread::get_id();
      
      void is_main_thread()
      {
             
          if (main_thread_id == std::this_thread::get_id())
              std::cout << "This is the main thread.\n";
          else
              std::cout << "This is not the main thread.\n";
      }
      
      int main()
      {
             
          is_main_thread();
      
          std::thread th(is_main_thread);
          th.join();
      
          return 0;
      }
      
    • joinable(),判断线程是否可以加入等待。
      void Func()
      {
             
          // do stuff...
      }
      
      int main()
      {
             
          thread t1;
          thread t2(Func);
          
          cout << "Joinable after construction:\n" << boolalpha;
          cout << "t1: " << t1.joinable() << '\n'; // false
          cout << "t2: " << t2.joinable() << '\n'; // true
      
          if (t1.joinable()) {
             
              cout << "t1 join" << endl; // 未执行
              t1.join();
          }
          if (t2.joinable()) {
             
              cout << "t2 join" << endl; // 执行了
              t2.join();
          }
      
          cout << "Joinable after joining:\n" << boolalpha;
          cout << "t1: " << t1.joinable() << '\n'; // false
          cout << "t2: " << t2.joinable() << '\n'; // false
      
          return 0;
      }
      
    • join():等该线程执行完成后才返回。
    • detach()
      • detach 调用之后,目标线程就成为了守护线程,驻留后台运行,与之关联的 std::thread 对象失去对目标线程的关联,无法再通过 std::thread 对象取得该线程的控制权。
  • 线程的创建(传入类函数)。
    class A {
         
    public:
        void AFunc(int num) {
         
            cout << this->name_ << ": AFunc num = " << num << endl; // zcoder: AFunc num = 100
        }
    
        void SetName(string name) {
         
            this->name_ = move(name)

相关推荐

  1. C++ 线

    2024-04-21 23:52:07       27 阅读
  2. C++ 线

    2024-04-21 23:52:07       19 阅读
  3. C#线

    2024-04-21 23:52:07       12 阅读
  4. C++ 线

    2024-04-21 23:52:07       12 阅读
  5. 线C#】

    2024-04-21 23:52:07       8 阅读
  6. C++ 线 atomic

    2024-04-21 23:52:07       46 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-21 23:52:07       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-21 23:52:07       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-21 23:52:07       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-21 23:52:07       20 阅读

热门阅读

  1. git远程仓库拉取超过1G报错解决办法

    2024-04-21 23:52:07       14 阅读
  2. Android 一键唤醒应用

    2024-04-21 23:52:07       17 阅读
  3. 搜索文件1.0

    2024-04-21 23:52:07       16 阅读
  4. ubuntu 监控查看硬件温度

    2024-04-21 23:52:07       13 阅读
  5. Android --- 布局与点击事件

    2024-04-21 23:52:07       15 阅读
  6. 适配器模式

    2024-04-21 23:52:07       12 阅读
  7. C++入门

    2024-04-21 23:52:07       15 阅读
  8. 基于单目相机的标靶三维定位——编程实现

    2024-04-21 23:52:07       15 阅读