2011-05-17 87 views
1

我正在使用C++进行项目工作。如何使用Boost库创建TimerHandler

我想被称为指定时间后TimerHandler,但在同一时间,我不希望阻止在下面的代码当前线程或io.run之后的任何代码():

#include <iostream> 
#include <string> 
#include <boost/format.hpp> 
#include <boost/asio.hpp> 
#include <boost/bind.hpp> 
#include <boost/date_time/posix_time/posix_time.hpp> 

class TimerTest 
{ 
public: 
    static void PrintOutTimerHandler(const boost::system::error_code&, const std::string& message) 
    { 
     std::cout << "PrintOutTimerHandler called: " << ", message: " << message << std::endl; 
    } 

    void run() 
    { 
     boost::asio::io_service io; 
     boost::asio::deadline_timer dt(io, boost::posix_time::seconds(5)); 

     std::cout << "Start:\t" << std::endl; 

     dt.async_wait(boost::bind(PrintOutTimerHandler, boost::asio::placeholders::error, std::string("here is the message"))); 

     // Do some job here 
     for (int i = 0; i < 1000000; ++i) 
      ++i, --i; 

     std::cout << "End:\t" << std::endl; 

     io.run(); 

     std::cout << "When to reach here 1: " << std::endl; 
    } 
}; 

int main() 
{ 
    TimerTest tt; 
    tt.run(); 

    std::cout << "When to reach here 2: " << std::endl; 

    return 0; 
} 

/* Current output: 
Start: 
End: 
PrintOutTimerHandler called: , message: here is the message 
When to reach here 1: 
When to reach here 2: 
*/ 

/* Expected output: 
Start: 
End: 
When to reach here 1: 
When to reach here 2: 
PrintOutTimerHandler called: , message: here is the message 
*/ 

我想我已经说清楚了。我的问题是:

  • 如果可以不 引入一个新的线程,诸如Flex 的ActionScript来解决,这是最好的,但 我想不会(我猜的ActionScript是 使用隐藏线);
  • 如果我们必须 引入一个额外的线程做 工作,你会介意为我写下 伪代码吗?

谢谢。

Peter

回答

2

这里是一个例子。在一个单独的线程

asio::io_service io_service; 
asio::thread t(boost::bind(&asio::io_service::run, &io_service)); 

运行io_service对象或线程组

boost::thread_group threads; 
for (std::size_t i = 0; i < my_thread_count; ++i) 
    threads.create_thread(boost::bind(&asio::io_service::run, &io_service)); 

在运行它记住,你的主线程应始终运行,因为当它的存在也催生了将退出所有线程。

我希望这会有所帮助。

+0

感谢您的回复。我试图将新线程应用于我的示例。但我不知道如何,请你帮我一下? – 2011-05-17 15:49:40

+0

@OcrunC,请忽略我上面的评论。我得到了它的工作。谢谢。 – 2011-05-17 21:31:15

+0

你会介意看看我的另一篇文章:http://stackoverflow.com/questions/6076524/why-my-eventtimer-implemented-by-boostasioio-service-and-boostdeadline-time – 2011-05-20 19:33:33

1

我误解了OrcunC说的,但实际上他是正确的。以下是修改后的版本供您参考:

#include <iostream> 
#include <string> 
#include <boost/format.hpp> 
#include <boost/asio.hpp> 
#include <boost/bind.hpp> 
#include <boost/thread.hpp> 
#include <boost/date_time/posix_time/posix_time.hpp> 

class TimerTest 
{ 
public: 
    static void PrintOutTimerHandler(const boost::system::error_code&, const std::string& message) 
    { 
     std::cout << "PrintOutTimerHandler called: " << ", message: " << message << std::endl; 
    } 

    TimerTest(unsigned int timeout) 
     : dt(io, boost::posix_time::milliseconds(timeout)) 
    { 
    } 

    void run() 
    { 
     std::cout << "Start:\t" << std::endl; 

     dt.async_wait(boost::bind(PrintOutTimerHandler, boost::asio::placeholders::error, std::string("here is the message"))); 

     boost::thread thrd(boost::bind(&boost::asio::io_service::run, &io)); 

     // Do some job here 
     for (int i = 0; i < 1000000; ++i) 
      ++i, --i; 

     std::cout << "End:\t" << std::endl; 

     std::cout << "When to reach here 1: " << std::endl; 
    } 

    boost::asio::io_service  io; 
    boost::asio::deadline_timer dt; 
}; 

int main() 
{ 
    TimerTest tt(5000); 
    tt.run(); 

    std::cout << "When to reach here 2: " << std::endl; 

    // Keep the main thread active for testing purpose. Otherwise, 
    // once the TimerTest object is destroyed when exiting the main() function, 
    // the sub thread spawed in tt.run() will also exit; 
    Sleep(10000); 
} 

/* Current output and Expected output: 
Start: 
End: 
When to reach here 1: 
When to reach here 2: 
PrintOutTimerHandler called: , message: here is the message 
*/ 
+0

只是一个补充 - CMakeLists .TXT需要建立上面的例子是: cmake_minimum_required(VERSION 2.8) 项目(OneShotTimer) find_package(升压1.40.0 REQUIRED系统线程) INCLUDE_DIRECTORIES($ {INCLUDE_DIRECTORIES} $ {Boost_INCLUDE_DIRS}) add_executable( OneShotTimer OneShotTimer.cpp) TARGET_LINK_LIBRARIES(OneShotTimer $ {Boost_LIBRARIES}) – 2013-12-18 16:23:53

+0

处理程序永远不会被我调用?我不得不改变睡眠(10000)到usleep(10000)来让它编译(我在Linux上),但我的输出是“开始:\t 结束:\t 何时到达这里1: 何时到达此处2:“ - 我从来没有看到”PrintOutTimerHandler叫:消息:这里是消息“就像你在输出中显示。 – 2013-12-18 16:35:43

相关问题