如何使用Boost库创建TimerHandler

时间:2011-05-17 05:07:55

标签: c++ boost timer boost-asio handler

我正在使用C ++开发一个项目。

我希望在指定时间后调用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是 使用隐藏的线程);
  • 如果必须的话 引入一个额外的线程来做 工作,你介意写下来吗? 伪代码给我?

感谢。

彼得

2 个答案:

答案 0 :(得分:2)

这是一个例子。在单独的线程中运行io_service

asio::io_service io_service;
asio::thread t(boost::bind(&asio::io_service::run, &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));

请记住,您的主线程应始终运行,因为当它存在时,所有生成的线程也将退出。

我希望这会有所帮助。

答案 1 :(得分: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
 */