码迷,mamicode.com
首页 > 编程语言 > 详细

c++11 线程

时间:2014-05-27 00:09:06      阅读:335      评论:0      收藏:0      [点我收藏+]

标签:style   c   class   blog   code   http   

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

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

  • 用成员函数来作线程函数,需要传入额外的对象值。如果需要传入参数,接在头两个参数后面。
  • 用引用而不同拷贝对象,需要调用 std::ref
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    #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> 确保对象存在,只要线程没死。
  • 1
    2
    3
    4
    5
    6
    int main()
    {
        std::shared_ptr<SayHello> p(new SayHello);
        std::thread t(&SayHello::greeting,p,"goodbye");
        t.join();
    }

      

c++11 线程,布布扣,bubuko.com

c++11 线程

标签:style   c   class   blog   code   http   

原文地址:http://www.cnblogs.com/bigben0123/p/3745027.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!