zoukankan      html  css  js  c++  java
  • C++11 std::thread在类的成员函数中的使用

    #include <thread>
    #include <iostream>
    
    class Wrapper {
      public:
          void member1() {
              std::cout << "i am member1" << std::endl;
          }
          void member2(const char *arg1, unsigned arg2) {
              std::cout << "i am member2 and my first arg is (" << arg1 << ") and second arg is (" << arg2 << ")" << std::endl;
          }
          std::thread member1Thread() {
              return std::thread(&Wrapper::member1, this);
          }
          std::thread member2Thread(const char *arg1, unsigned arg2) {
              return std::thread(&Wrapper::member2, this, arg1, arg2);
          }
    };
    
    int main() {
      Wrapper *w = new Wrapper();
      std::thread tw1 = w->member1Thread();
      tw1.join();
      w->member2Thread("hello", 100).detach();

    return 0;
    }

    join()为主线程等待子线程的阻塞模式

    detach()为主线程不管子线程的非阻塞模式

  • 相关阅读:
    qt学习笔记(1):qt点击运行没有反应。
    JS Object类型
    JS Boolean数据类型和数据类型转换规律
    CSS雪碧图
    CSS
    PS基础
    JS number数字类型
    js中的变量和数据类型
    JS 基础
    单词
  • 原文地址:https://www.cnblogs.com/c4isr/p/9205164.html
Copyright © 2011-2022 走看看