Advertisement

How do you implement controllable delays in IOCP system

Started by December 22, 2003 08:27 AM
2 comments, last by lisitsin 21 years, 1 month ago
Hi, The task is as follows: I have a pool of threads waiting on IOCP. Users are playing multiple games, each game is a state-machine. Occasionally the game needs to make a delay, say, for 5 seconds, then notify participating clients. How do you do this? I though about using WM_TIMER or XXXWaitableTimer, but: 1) They require the thread to be in an alertable state - but the threads are waiting for I/O completion, which is not the case. 2) Completion routine is called in the thread that called SetTimer or SetWaitableTimer - not good. I though about introducing additional thread that waits for timers and posts completion packets onto the IOCP. I also thought about calling GetQueuedCompletionStatus with a timeout, then waitfortimers - again, with a timeout. Both solutions seem to be error-prone. Is there an option to solve the problem more elegantly? Thank you, refa
Tough call. Using the timeout value in the GetQueuedCompletionStatus() call makes it available to all the pending threads (I know because I tried it). The best way would be to create a 'timer thread' that waits on an event with a timeout. The timeout implements the shortest delay between 'now' and the closest timer's expiration, and the event is there to announce you've added one more delay in the list of delays to process or you want to close the thread. When there are no timers, use the INFINITE (0xFFFFFFFFL) value to make it wait indefinitely until the event is triggered.

for(;;){   timeout = _Get_My_Shortest_Delay_From_List();   status = ::WaitForSingleObject( event, timeout );   if ( thread_is_done )      return;   _Process_All_Expired_Timers_In_List()}


The thread will be asleep most of the time and only be active when a) you want to insert a new delay in the list, or b) a timer has expired and need to be processed, or c) when the event is triggered to exit the thread. Just make sure that the code surrounding the timer list is thread-safe with critical section locks.

Hope this helps.

-cb

[edited by - cbenoi1 on December 23, 2003 9:33:36 AM]
Advertisement
Okay.

There is a thread pool and the threads process IOCP. Now, there are multiple games.

When you say delay, which game do you want to delay?

Kuphryn
cbenoi1

Thanks, I guess i''ll have to live with that

kuphryn

The game knows when to delay itself.

Example: one of the multiple games receives a message from one of the clients, say, client1, processes that message, and finds out that client1 winds over client2. It transfers "WOW! Client1 wins! Fatality!". Then it delays itself for 5 seconds to give the clients some time to think over what happened before starting the next round.

This topic is closed to new replies.

Advertisement