我正在尝试在C++中实现一个回调计时器函数,它应该在超时后进行回调。我在一个套接字上监听,然后在上面等待消息。因此,如果我有像getMessages()这样的东西,并且我想将计时器函数作为参数传递给它。那么,如何实现在接收到消息之后,如果在下一条消息到达之前存在超时,则启动计时器并进行回调。
我是回调函数的新手。任何帮助都是非常感谢的。谢谢!
发布于 2014-08-18 16:18:21
您可能知道,但是C++中没有回调函数这样的东西。但是,也有函数指针。
函数指针实际上就是指向函数的指针。对于"normal“指针,您可以指定指针所指向的数据类型(例如int *foo
、char *bar
...);函数指针的工作方式与此类似,因为您可以指定函数的签名:void (*baz)(int)
。请注意,在本例中,baz
是指针的名称。
没有代码,我不知道你的计时系统是如何工作的。首先想到的是一个被重复调用的tick函数。您可以定期调用此函数,也可以测量两次函数调用之间的时间-在这两种情况下,您都知道已经过的时间。递增(或递减)分配的时间变量,当时间到期时,使用函数指针来触发“回调”。
你可能会发现有一件事很有用,那就是像这样typedef
你的回调函数:typedef void (*Callback)(int)
(同样,Callback
是标识符)。然后,您可以像定义任何其他成员(Callback func = foo
)一样定义一个函数并调用它(func(12)
)。
PS: SE周围有很好的函数指针资源。Here's one.
发布于 2014-08-18 20:20:47
Here is one approach that I would suggest. You will require 3 different threads for this:
1. First thread is the main processing thread (Th1). It waits for two separate events - either "data on socket" event or "timeout event"
2. Second thread (Th2) will constantly listen on socket ( stay blocked on socket either through select or read call ) and when data appears it will read the data, wrap the data in an event and post it to the first thread.
3. The third thread (Th3) will implement the timer functionality. It will wake up after every 'N' seconds ( where 'N'is timeout interval ) and once it wakes up it will create a timeout event and post it to first thread.
A skeleton of the code would look something like this:
Class Processor
{
public:
// std::list<ProcessorEventBase* > eventQueue;
void init ()
{
// 1. initialize SocketListener and pass address of 'Processor::recvDataOnSocket' function
// 2. initialize TimerUtil and pass address of 'Processor::timeOut' function
// 3. create and run Processor ( Th1 ) thread
// 4. keep the thread waiting on a condition variable
// 5. When thread wakes up, let it fetch event from queue and process it.
}
static void recvDataOnSocket ( char* data, int datalen, void* arg )
{
// typecast arg to Processor
// create a data on socket event
// post it to the 'eventQueue'
// signal the waiting thread
}
static void timeOut ( void* arg )
{
// typecast arg to Processor
// create a timeout event
// post it to event queue
// signal the waiting thread
}
}
class SocketListener
{
public:
void init ( SocketCallback socketCallback)
{
// store the socket Callback
// create a thread to listen on socket ( Th2 )
// when data arrives, call the callback i.e. socketCallback and
// pass data pointer and its size and the stored arguement
}
}
class TimerUtil
{
public:
void init ( TimeoutCallback timeoutCallback, void* arg )
{
// store the timeout Callback
// create a thread that wakes up every 'N' seconds ( Th3 )
// when timer expires, call the callback i.e. timeoutCallback and
// pass the stored argument i.e. arg
}
}
Callback would be something like this:
typedef void (*SocketCallback ) ( char* data, int datalen, void* arg);
typedef void (*TimeoutCallback ) ( void* arg );
https://stackoverflow.com/questions/25367464
复制相似问题