2012-05-12 153 views
0

我正在学习Boost线程和Asio(异步输入/输出),并且我写了下面的示例将一些概念放在一起。提升线程和提升Asio

class Worker { 
    private: 
    boost::asio::io_service&  m_ios; 
    boost::asio::deadline_timer m_timer; 
    unsigned      m_cycles; 
    unsigned      m_time2wait; 
    std::string     m_threadName; 

    public: 
    Worker(boost::asio::io_service& ios, 
      unsigned cycles, unsigned time2wait, const std::string& name); 
    ~Worker(); 
    void start(); 
    void run(); 
    void stop(); 
}; 


Worker::Worker (boost::asio::io_service& ios, 
       unsigned cycles, unsigned time2wait, const std::string& name) 
: m_ios(ios), 
    m_timer(ios, boost::posix_time::seconds (1)), 
    m_cycles(cycles), 
    m_time2wait(time2wait), 
    m_threadName(name) 
{ 
    logMsg(m_threadName, "is starting . . . "); 
} 

Worker::~Worker() 
{ 
    logMsg(m_threadName, "is ending . . . "); 
} 

void Worker::start() 
{ 
    logMsg (m_threadName, "start was called"); 

    m_timer.expires_at (m_timer.expires_at() + 
       boost::posix_time::seconds (m_time2wait)); 
    m_timer.async_wait (boost::bind (&Worker::run, this)); 
} 

void Worker::stop() 
{ 
} 

void Worker::run() 
{ 
    if (m_cycles > 0) 
    { 
    logMsg (m_threadName, "run # ", m_cycles); 
    --m_cycles; 
    m_timer.expires_at (m_timer.expires_at() + 
         boost::posix_time::seconds(m_time2wait)); 
    m_timer.async_wait(boost::bind(&Worker::run, this)); 
    } 
    else { 
    logMsg (m_threadName, "end of cycling"); 
    } 
} 

void run_threads (boost::asio::io_service& io_srv) 
{ 
    Worker worker_1(io_srv, 5, 2, "worker 1"); 
    Worker worker_2(io_srv, 5, 4, "worker 2"); 
    worker_1.start(); 
    worker_2.start(); 

    boost::shared_ptr <boost::thread> worker_Thread_One (
    new boost::thread (boost::bind (&boost::asio::io_service::run, &io_srv))); 

    boost::shared_ptr <boost::thread> worker_Thread_Two (
    new boost::thread(boost::bind(&boost::asio::io_service::run, &io_srv))); 

    worker_Thread_One->join(); 
    worker_Thread_Two->join(); 
} 

int main (int argc, char** argv) 
{ 
    boost::asio::io_service ios; 

    run_threads(ios); 

    return 0; 
} 

我试图让一些线程并行工作,每个线程通过一个特定的对象来完成它的工作。这个例子显然似乎工作,但我觉得我错了混合线程和Asio(坏设计)。是否有线程与Asio一起工作的正确方法(一个用于多线程的io_service)?

线程对象和“worker”对象如何“绑定”在一起?我认为他们不是我所愿。例如,如果我实例化两个对象和两个线程,我有预期的输出。如果我实例化了两个对象和一个线程,则程序的输出是相同的。

(该logMsg是一个简单的COUT包装用互斥同步的输出操作)。

+0

当你问一个问题时,尽量保持信息。 – Ankit

+0

代码看起来很理智。我可能会使用system_timer而不是deadline_timer。 deadline_timer会做一堆日历工作,而system_timer只是在“ticks”中工作 – RichardBruce

回答

0

你的例子看起来是正确的,但它没有必要在这种情况下都使用ASIO。当您使用io_service作为线程池的工作队列时,Asio +线程最为有用。如果你想以这种方式使用它,请调查io_service::post

0

asio::io_service只是一个系统任务的经销商,它拥有一个任务队列并对其进行处理,你不需要像这样使用它,你应该创建一个包含你自己功能的线程,其中包括io_service::run()。 线程和io_service可以是一对一或多对一的。