简体   繁体   English

管理自己的boost :: thread的Singleton对象

[英]Singleton object that manages its own boost::thread

Singleton.h 单例

class Singleton{
public:
 static Singleton &getInstance() {
      static Singleton instance;
      return instance;
 }
 void start();
 void run();
 void join();

private:
 Singleton();
 int a;
 boost::thread thread;
};

Singleton.cpp 单例

Singleton()::Singleton:a(0){}
void Singleton::run(){ a=1; }
void Singleton::start(){ thread = boost::thread(&Singleton::run, this, NULL); }
void Singleton::join(){ thread.join(); }

main.cpp main.cpp

Singleton::getInstance().start();
Singleton::getInstance().join();

The error I get is 我得到的错误是

/usr/include/boost/bind/bind.hpp: In instantiation of 'void boost::_bi::list2::operator()(boost::_bi::type, F&, A&, int) [with F = void (Singleton:: )(); /usr/include/boost/bind/bind.bin:在'void boost :: _ bi :: list2 :: operator()(boost :: _ bi :: type,F&,A&,int)的实例中[F等于void (Singleton :: )(); A = boost::_bi::list0; A = boost :: _ bi :: list0; A1 = boost::_bi::value; A1 = boost :: _ bi :: value; A2 = boost::_bi::value]': /usr/include/boost/bind/bind_template.hpp:20:59: required from 'boost::_bi::bind_t::result_type boost::_bi::bind_t::operator()() [with R = void; A2 = boost :: _ bi :: value]':/usr/include/boost/bind/bind_template.hpp:20:59:需要'boost :: _ bi :: bind_t :: result_type boost :: _ bi :: bind_t: :operator()()[with R = void; F = void (Singleton:: )(); F = void(Singleton :: )(); L = boost::_bi::list2, boost::_bi::value >; L = boost :: _ bi :: list2,boost :: _ bi :: value>; boost::_bi::bind_t::result_type = void]' /usr/include/boost/thread/detail/thread.hpp:117:17: required from 'void boost::detail::thread_data::run() [with F = boost::_bi::bind_t, boost::_bi::value > >]' Singleton.cpp:4:1: required from here boost :: _ bi :: bind_t :: result_type = void]'/usr/include/boost/thread/detail/thread.hpp:117:17:需要'void boost :: detail :: thread_data :: run()[ F = boost :: _ bi :: bind_t,boost :: _ bi :: value>>]'Singleton.cpp:4:1:从此处开始

I'm stuck and not sure what to do here, Thanks for your help. 我很困惑,不确定在这里做什么,谢谢您的帮助。

Given that your post is tagged C++11 , I've taken the liberty of using its features. 鉴于您的帖子被标记为C ++ 11 ,我已自由使用了其功能。 I've implemented your code without issue: 我已经实现了您的代码,没有任何问题:

#include <thread>

class Singleton{
public:
   static Singleton &getInstance() 
   {
      static Singleton instance;
      return instance;
   }

   void start()
   {
      thread = std::thread(
         [this]() // lambda to call internal run method
         {
             run();
         });
   }

   void run()
   {
      a=1;
   }

   void join()
   {
      thread.join();
   }


private:
   Singleton() = default;
   int a = 0;
   std::thread thread;
};

Live Demo 现场演示

Notice I use std::thread , defaulted constructor, a lambda, and an in-class member initializer. 注意,我使用了std::thread ,默认构造函数,一个lambda和一个类内成员初始化器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM