Hi Amos, thank you very much for the clear and very informative reply. It seems I need some learning before this implementation and I'll do that right away based on your advises. Rafi. -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Amos Shapira Sent: Tuesday, January 09, 2007 1:23 AM To: Linux-IL Subject: Re: implement timers in a multi thread application
On 09/01/07, Rafi Cohen <[EMAIL PROTECTED]> wrote: Gilad, thank you very much for your advise. I tend to implement it, but I still remain with the following question: if a thread has various timeout cases, for example, one after 10 seconds and another one after 40 seconds, how this could be implemented in the timers thread waiting for the SIGALRM signal upon sigwait() call? You should sort of implement this as a little OS scheduler - pass a timer and a function to call to when the timer expires into a shared timer queue, regardless of which thread the scheduling is done from. Once the timer expires the main timer-handling thread should arrange that some "worker thread" will execute the function handed over to handle that timer expiry. When alarm is called, for example with 10 as parameters, I know that SIGALRM will be received after (aproximately) 10 seconds. But, if I don't use the alarm call, then what? Is there a way to produce SIGALRM signal every constant number of seconds so that I can count the number of received SIGALRM signals and proceed according to the number that once sum up to 10 seconds and once sums up to 40 seconds? No, what you should do is to implement a timer-queue - a shared linked list of timer events, each entry basically has a <time left from now, function to call, void *> ttiplet. That list should be ordered by increasing <time left from now>, each entry actually holding the difference in time between it and the one before it. The "current alarm" will be the first in the list and when the ALRM signal is received the "timer scheduling thread" can check that it's the right time to run the first entry in the list (passing it the <void *> given when the timer was added to the list) then clear it from the list and set the next alarm for the next timer on the list. The <void *> will be extremely important to provide context for the function which is being called when the timer expires. (Small implementation note - take into account possibility of multiple timers expiring at the same time, threads manipulating the list concurrently, and the list being changed while the timer thread is processing an alarm ( e.g. a function called from an expired timer adding a timer to call itself in a few more seconds). I've done this before (without threads, the POSIX standard wasn't commonly available back then) and it's quite fun to program, but today I'd recommend you to consider moving to C++ and maybe take advantage of libraries like Boost ( boost.org) or ACE (http://www.cs.wustl.edu/~schmidt/ACE.html, look for "Concurrency" in http://www.dre.vanderbilt.edu/~schmidt/DOC_ROOT/ACE/docs/ACE-categories.html for a start) I hope I was clear here. Thanks, Rafi. HTH, --Amos -- No virus found in this outgoing message. Checked by AVG Free Edition. Version: 7.5.432 / Virus Database: 268.16.7/620 - Release Date: 1/8/2007 4:12 PM