Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++中的回调定时器功能 我尝试在C++中实现一个回调定时器函数,它在超时后应该进行回调。我正在监听一个套接字,然后在上面等待消息。所以,如果我有一些像getMessages的东西,我想把timer函数作为参数传递给它。那么,如何实现在接收到消息之后,启动计时器,如果在下一条消息到达之前有一个超时,那么回调就会发生呢_C++_Timer_Callback - Fatal编程技术网

C++中的回调定时器功能 我尝试在C++中实现一个回调定时器函数,它在超时后应该进行回调。我正在监听一个套接字,然后在上面等待消息。所以,如果我有一些像getMessages的东西,我想把timer函数作为参数传递给它。那么,如何实现在接收到消息之后,启动计时器,如果在下一条消息到达之前有一个超时,那么回调就会发生呢

C++中的回调定时器功能 我尝试在C++中实现一个回调定时器函数,它在超时后应该进行回调。我正在监听一个套接字,然后在上面等待消息。所以,如果我有一些像getMessages的东西,我想把timer函数作为参数传递给它。那么,如何实现在接收到消息之后,启动计时器,如果在下一条消息到达之前有一个超时,那么回调就会发生呢,c++,timer,callback,C++,Timer,Callback,我不熟悉回调函数。非常感谢您的帮助。谢谢 >你可能知道,但是C++中没有回调函数。但是,也有函数指针 函数指针实际上是指向函数的指针。对于普通指针,指定指针指向的数据类型,例如int*foo、char*bar。。。;函数指针的工作原理类似于指定函数的签名:void*bazint。请注意,在本例中,baz是指针的名称 如果没有代码,我不确定你的计时系统将如何工作。首先想到的是一个被反复调用的tick函数。您可以定期调用此函数,也可以测量函数调用之间的时间-在任何一种情况下,您都知道所经过的时间。递

我不熟悉回调函数。非常感谢您的帮助。谢谢

>你可能知道,但是C++中没有回调函数。但是,也有函数指针

函数指针实际上是指向函数的指针。对于普通指针,指定指针指向的数据类型,例如int*foo、char*bar。。。;函数指针的工作原理类似于指定函数的签名:void*bazint。请注意,在本例中,baz是指针的名称

如果没有代码,我不确定你的计时系统将如何工作。首先想到的是一个被反复调用的tick函数。您可以定期调用此函数,也可以测量函数调用之间的时间-在任何一种情况下,您都知道所经过的时间。递增或递减分配的时间变量,当时间结束时,使用函数指针触发回调

您可能会发现一件有用的事情是对回调函数进行类型定义,如下所示: typedef void*Callbackint再次,Callback是标识符。然后,您可以定义一个类似于任何其他成员回调func=foo的函数,并将其称为func12

PS:SE周围有很多关于函数指针的好资源

    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 );