定时函数调用 我在C++、Linux环境下编程。我怎样才能在某个时间间隔后以异步方式调用函数,同时也以常规时间间隔调用函数?我发现升压定时器在。但是,它需要调用io.run()来调用回调,这反过来又让我回到了最初的问题,即需要我处理调用函数所花费的时间。我需要的是类似于C#System.Threading.Timer的东西,它将在指定的时间段后回调我传递的函数

定时函数调用 我在C++、Linux环境下编程。我怎样才能在某个时间间隔后以异步方式调用函数,同时也以常规时间间隔调用函数?我发现升压定时器在。但是,它需要调用io.run()来调用回调,这反过来又让我回到了最初的问题,即需要我处理调用函数所花费的时间。我需要的是类似于C#System.Threading.Timer的东西,它将在指定的时间段后回调我传递的函数,c++,linux,C++,Linux,我正在考虑为每个函数调用创建一个线程。然而,我有很多这样的计时器回调。因此,我担心创建这样的线程会很昂贵,或者我有其他选择吗 谢谢。一个选项是切换到或用于他们的事件循环,并使用该循环编写代码。asioio\U服务在其自己的线程中运行,一旦您安排了计时器事件,它将在适当的时间给您回电话 下面是一个完整的示例: #include <iostream> #include <boost/asio.hpp> #include <boost/bind.hpp> usin

我正在考虑为每个函数调用创建一个线程。然而,我有很多这样的计时器回调。因此,我担心创建这样的线程会很昂贵,或者我有其他选择吗


谢谢。

一个选项是切换到或用于他们的事件循环,并使用该循环编写代码。

asio
io\U服务在其自己的线程中运行,一旦您安排了计时器事件,它将在适当的时间给您回电话

下面是一个完整的示例:

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>

using namespace std;

class timed_class
{
public:
  timed_class(boost::asio::io_service& io_service) : _timer(io_service)
  {
    // now schedule the first timer.

    _timer.expires_from_now(boost::posix_time::milliseconds(100)); // runs every 100 ms
    _timer.async_wait(boost::bind(&timed_class::handle_timeout, this, boost::asio::placeholders::error));
  }

  void handle_timeout(boost::system::error_code const& cError)
  {
    if (cError.value() == boost::asio::error::operation_aborted)
      return;

    if (cError && cError.value() != boost::asio::error::operation_aborted)
      return; // throw an exception?

    cout << "timer expired" << endl;

    // Schedule the timer again...
    _timer.expires_from_now(boost::posix_time::milliseconds(100)); // runs every 100 ms
    _timer.async_wait(boost::bind(&timed_class::handle_timeout, this, boost::asio::placeholders::error));
  }

private:
  boost::asio::deadline_timer _timer;
};


int main(void)
{
  boost::asio::io_service io_service;

  timed_class t(io_service);

  io_service.run();

  return 0;
}

而不是
io_service.run()
,现在主线程将继续,一旦超时过期,
io\U服务
将调用
handle\U timeout

伪代码以获得最简单的解决方案:

function doThis() {
    // your code here
}

function threadHandler() {
    sleep(SecondsUntilTime);
    doThis();
}

function main () {
    anotherthread = thread_create(threadHandler);

    // more code here

    join(anotherthread);
    exit();
}

你的休息时间有多短?当然,如果你愿意降到C,还有很多。谢谢你的回复。我的问题是我需要调用主线程中的io_service.run()。但是,在timed_类t(io_服务)和io_服务.run()之间的代码中,我需要执行其他任务。如果我先调用io_service.run(),它会在执行任务之前先等待延迟。如果我在io_service.run()之前调用函数来执行任务,我最终需要知道任务完成所需的时间,以便在适当的时间调用io_service.run(),这是我最初的问题。所以无论哪种方法都不能解决我的问题。还是我遗漏了什么?谢谢。@Ashley,
io\u服务::run()
正在阻塞,同时有事件要分派,如果您需要在需要定期执行的代码之前进行一些设置,您应该先进行设置,然后调用
run()
定期调用
handle\u timer
函数。如果要执行的代码是需要定期调用的代码,请将其放入
timed_类
中的函数中,然后在ctor和
handle_timeout
正文中调用。如果你放一些你想要实现的伪代码可能会更好…谢谢你的回复。让我提供基本情况。假设我只需要在300毫秒后调用Function一次。我想将Function传递到某个计时器回调中,该回调将在300毫秒(T1)后调用Function,类似于上面MacGuy的解决方案所提供的。在当前时间(T0),如果调用io_service::run(),它将阻塞到(T1),这是不可取的,因为我不希望主线程阻塞。如果我稍后调用io_service::run(),我无法估计什么时候正好是300毫秒之后,这是300毫秒之后调用函数的原始问题。如果不希望主线程阻塞,那么在单独的线程中调用
io_service::run
,我已经将代码放在上面了。。。
function doThis() {
    // your code here
}

function threadHandler() {
    sleep(SecondsUntilTime);
    doThis();
}

function main () {
    anotherthread = thread_create(threadHandler);

    // more code here

    join(anotherthread);
    exit();
}