Rafi Cohen wrote:

My application is a multithread one, for which each thread has it's own timer and tasks upon timeout. Each timer may be different (varies from 10 seconds in one case to 24 hours in another). Each timer should also be independent and the threads should run concurrently withou actually affecting other threads and timers.

POSIX only supply a single timer counting in real time for each process. Multiple timers are either created by the programmer based on this single timer using a timer heap or the same done by the threading library pthread create_timer.

It seems that there still is some effect of some timers to others and I don't achieve the goal of total independency of timers.

There is just one timer.

Here is the strategy I decided to use, by the way, my code is in C:
Each thread uses in a loop the function pthread_cond_timedwait. Part of them may exit either upon signal or time out and part of them actually waits until timeout.

This is not a timer. It's a blocking system call with a timeout.

The problem I think I see is the timer adjustment prior to using pthread_cond_timedwait.

What timer? you're using a blocking system call with a timeout.

I sould also note that each thread has it's own and unique mutex and condition variables for this function. The time adjustment is done by calling clock_gettime with CLOCK_REALTIME as it's first parameter. This is called before each use of pthread_cond_timedwait and immediately after call to clock_gettime I add the timeout seconds to the tv_sec field of the timespec structure.
Now, to my questions:
1. Does this strategy seem correct, if not please give other ideas.

That's how it is supposed to be used

2. Specifically, is the function clock_gettime the correct one to use, should it's first parameter be the one I use and should it be called indeed before each pthread_cond_timedwait within the loop, or only once before the loop.

Anyway, clock_gettime is the correct one. CLOCK_REALTIME is correct and you need to call it only once before entering the loop.

The man page has an excellent code example code:

http://linux.die.net/man/3/pthread_cond_timedwait

Search for "Timed Condition Wait"

I'll be glad to have detailed ideas, in case you think I'm wrong here.

I don't think I understand what you're trying to achieve exactly.

3. Shachar (Shemes), you pointed me once to the libevent library as an alternative. I looked into this library and was very willing to use it. However, I understood from it's documentation that it is not threadsafe. Therefore, it seems not to be the right idea to use it for concurrent timers.

Timers?
"You keep saying that word. I don't think you know what it means..." :-)

Am I wrong here? I'll be glad to stand corrected and use such option instead of the strategy I mentioned above.
Any assistance will be most appreciated.

A description of what you are trying to do will do wonders here, I think.

And why threads, anyway?

Note: I raise programming issues here from time to time and get good answers most of the time. However, probably there are more linux users than programmers here. So, if you think there should be a better forum or mailing list to raise such questions, then please let me know. I do think however, that there are here really knowledgeable people that may and I believe will help the best they can.

This happens to be the most technical Linux oriented mailing list in Israel.

gilad

--
Gilad Ben-Yossef <[EMAIL PROTECTED]>
Codefidence. A name you can trust(tm)
Web: http://codefidence.com  | SIP: [EMAIL PROTECTED]
IL: +972.3.7515563 ext. 201  | Fax:            +972.3.7515503
US: +1.212.2026643 ext. 201  | Cel:           +972.52.8260388

        "There once was a virtualization coder,
         Whose patches kept getting older,
         Each time upstream would drop,
         His documentation would slightly rot,
         SO APPLY MY F*$KING PATCHES OR I'LL KEEP WRITING LIMERICKS."
                -- Rusty Russel



=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]

Reply via email to