6.Timer Queue (A module that operates tick event from thread pool)

Windows XP, 2000 or higher version of Operating System support API called Windows Timer Queue. ProudNet also has a corresponding API that allows you to use that function much easily.

Timer Queue runs a function designated by user in scheduled time from one of threads in Thread pool. If every thread is busy operating something (running state) then the execution of function gets held until any thread becomes available.

When user function is called from Timer queue, one of threads in thread pool gets appointed to execute. But even though the execution of user function that was previously running hasn't been completed, a thread in idle state will be appointed to execute the function.

Let assum there is a task list as followed. The black arrow means 0.1 second and A,B,C,D,E are the tasks that need to be worked on at every 0.1 second. A,D get completed within 0.1 second, B gets completed right on 0.1 sec and C,E take more than 0.1 second.

그림 6-1A list of tasks that needs to be executed in scheduled time

If it is in 10. Understanding server main loop method, then task list operates as shown in the diagram below. Since all tasks are being executed in a single thread, D and E can't start on time.

그림 6-2When all tasks are being executed from a single thread

However, in Timer Queue method, there will be another thread appointed for operation of D and E will be executed from the thread completed the operation of C.

그림 6-3When tasks are being executed in Timer Queue method

The biggest advantage of using Timer Queue is that it simply adds more threads for the need of executing tasks on schedule. In another words, more than two threads get appointed to operate user function simultaneously. Taking leverage on this feature, Timer Queue is being mainly used in server program.

Timer Queue enables parallelism but must bear a risk of having parallelism. Thus, please judge carefully whether or not parallelism of Timer Queue is needed in your project.

(WARNING: If Timer Queue is misused, it could cause overload to threads of server process that may lead to slow down of performace! If you don't need to use Timer Queue then it is more effective to use Proud.CTimerThread or 16. Managing Timer loop, RMI, Event from server.)

In order to use Timer queue, you need to access Proud.CTimerQueue class, which is singleton. Proud.CTimerQueueTimer objective will return when setting a function that is going to be called on a regular basis & calling cycle at Proud::NewTimerParam structure and inserting them into Proud.CTimerQueue.NewTimer’s parameter. And user function will be timely executed until Proud.CTimerQueueTimer object gets destroyed.

VOID NTAPI UserFunction(void* context,BOOLEAN TimerOrWaitFired)
{
    // User Function
}

void Foo()
{
    ...
    // Structure variable declaration
    NewTimerParam p1;
    
    // User function Setting.
    p1.m_callback = UserFunction;
    // Set pointer that shall be sent to parameter.
    p1.m_pCtx = NULL;
    // Set as callback shall start after 1 second.
    p1.m_DueTime = 1000;
    // Set as callback shall be done at an interval of 0.1 second.
    p1.m_period = 100;
    
    // User function gets called from thread pool in evey 0.1 second on schedule time.
    Proud::CTimerQueueTimer* ret = Proud::CTimerQueue::Instance().NewTimer(p1);
    
    ...
    
    // Destroy timer object. After its destruction, user function won't be called again.
    delete ret;
}