如何使用 Boost 库创建计时器处理程序

How To Create TimerHandler Using Boost Library

本文关键字:计时器 处理 程序 创建 何使用 Boost      更新时间:2023-10-16

我正在使用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
 */

我想我说得很清楚。 我的问题是:

  • 如果这可以解决,没有引入一个新线程,如 FlexActionScript,这是最好的,但是我猜不是(我猜ActionScript是使用隐藏线程);
  • 如果我们必须引入一个额外的线程来执行乔布斯,你介意写下我的伪代码?

谢谢。

彼得

这是一个示例。在单独的线程中运行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));

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

我希望这有所帮助。

我误解了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
 */