首页 > 编程语言 > 详细

C++11 std::thread在类的成员函数中的使用

时间:2018-06-20 20:12:21      阅读:1190      评论:0      收藏:0      [点我收藏+]
#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()为主线程不管子线程的非阻塞模式

 

C++11 std::thread在类的成员函数中的使用

原文:https://www.cnblogs.com/c4isr/p/9205164.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!