我想在C++中实现一个系统,以便我可以调用一个函数并要求在X毫秒后调用另一个函数。类似这样:
callfunctiontimed(25, funcName);
25代表在调用函数之前应等待的毫秒数。
我想知道是否需要使用多线程,并使用一些延迟函数?除了使用函数指针外,这样的功能是如何工作的?
我想在C++中实现一个系统,以便我可以调用一个函数并要求在X毫秒后调用另一个函数。类似这样:
callfunctiontimed(25, funcName);
25代表在调用函数之前应等待的毫秒数。
我想知道是否需要使用多线程,并使用一些延迟函数?除了使用函数指针外,这样的功能是如何工作的?
t.expires_from_now(boost::posix_time::seconds(1));
为了满足您的需求,需要在200毫秒后进行函数调用。
t.expires_from_now(boost::posix_time::milliseconds(200));
以下是一个完整的工作示例。它正在重复调用,但我认为只需稍微更改就可以轻松地仅调用一次。
#include <iostream>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost::asio;
using namespace std;
class Deadline
{
public:
Deadline(deadline_timer &timer) : t(timer) {
wait();
}
void timeout(const boost::system::error_code &e) {
if (e)
return;
cout << "tick" << endl;
wait();
}
void cancel() {
t.cancel();
}
private:
void wait() {
t.expires_from_now(boost::posix_time::seconds(1)); //repeat rate here
t.async_wait(boost::bind(&Deadline::timeout, this, boost::asio::placeholders::error));
}
deadline_timer &t;
};
class CancelDeadline {
public:
CancelDeadline(Deadline &d) :dl(d) { }
void operator()() {
string cancel;
cin >> cancel;
dl.cancel();
return;
}
private:
Deadline &dl;
};
int main()
{
io_service io;
deadline_timer t(io);
Deadline d(t);
CancelDeadline cd(d);
boost::thread thr1(cd);
io.run();
return 0;
}
//result:
//it keeps printing tick every second until you enter cancel and enter in the console
tick
tick
tick
您希望它异步执行,以便在不阻塞主执行线程的情况下在25毫秒后执行回调函数吗?如果是这样,您可以在您实现的计时器/定时回调函数中从一个单独的线程执行回调函数。
如果您不使用多线程,则在运行睡眠/ usleep 时,您的主要或调用callfunctiontimed(25,funcName)的函数将被阻塞。现在由您决定要实现什么行为。
真正的解决方案不会像多线程或不多线程那么简单。例如,如何考虑到该函数可以多次调用具有不同超时和函数的不同计时器/回调信息。
一种方法是这样的:
As the timer thread is done sleeping, it removes and looks at head of the list and executes the function pointer in a new thread. Timer thread is re-initialized with sleep time on the new head of the list.
main() {
//spawn a timer thread with pthread create
callfunctiontimed(25, test);
callfunctiontimed(35, show);
callfunctiontimed(4, print);
}
callfunctionTImed(int time, (func*)function, void*data) //
{
//add information to sorted list of timer and callbacks
//re-initialize sleep_time for timer thread if needed.
return.
}
timerThread() {
while(1){
sleep(sleep_time);
//look at head of timer list, remove it, call function in a new thread
//adjust sleep time as per new head
}
}
fork()
),并且在主任务(进程)中使用pthread
,并使用POSIX共享内存和POSIX消息队列在进程之间进行通信。SetTimer
的函数 - http://msdn.microsoft.com/en-us/library/windows/desktop/ms644906(v=vs.85).aspx
示例代码:#define STRICT 1
#include <windows.h>
#include <iostream.h>
VOID CALLBACK TimerProc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime)
{
cout << "CALLBACK " << dwTime << '\n';
cout.flush();
}
int main(int argc, char *argv[], char *envp[])
{
int Counter=0;
MSG Msg;
UINT TimerId = SetTimer(NULL, 0, 2000, &TimerProc); //2000 milliseconds
cout << "TimerId: " << TimerId << '\n';
if (!TimerId)
return 16;
while (GetMessage(&Msg, NULL, 0, 0))
{
++Counter;
if (Msg.message == WM_TIMER)
cout << "Counter: " << Counter << "; timer message\n";
else
cout << "Counter: " << Counter << "; message: " << Msg.message << '\n';
DispatchMessage(&Msg);
}
KillTimer(NULL, TimerId);
return 0;
}