zoukankan      html  css  js  c++  java
  • c++11 线程

    转自:http://www.justsoftwaresolutions.co.uk/threading/multithreading-in-c++0x-part-3.html

    是个just的c++库。和c11很像。

    • 用成员函数来作线程函数,需要传入额外的对象值。如果需要传入参数,接在头两个参数后面。
    • 用引用而不同拷贝对象,需要调用 std::ref
    • #include <thread>
      #include <iostream>
      
      class SayHello
      {
      public:
          void greeting(std::string const& message) const
          {
              std::cout<<message<<std::endl;
          }
      };
      
      int main()
      {
          SayHello x;
          std::thread t(&SayHello::greeting,&x,"goodbye");
          t.join();
      }
      

        

    • 栈上的对象,需要确保生命期比thread长。否则可以用  std::shared_ptr<SayHello> 确保对象存在,只要线程没死。
    • int main()
      {
          std::shared_ptr<SayHello> p(new SayHello);
          std::thread t(&SayHello::greeting,p,"goodbye");
          t.join();
      }
      

        

  • 相关阅读:
    FastDFS概述及原理
    SpringBoot中使用Redis缓存注解
    SpringBoot中使用Redis
    Jedis
    Redis客户端
    Redis的集群配置
    Redis的复制
    英语笔记-5
    内网转发Ubuntu
    数学笔记-4
  • 原文地址:https://www.cnblogs.com/bigben0123/p/3745027.html
Copyright © 2011-2022 走看看