Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to implement thread specific timer in a multi-threaded program

309 views
Skip to first unread message

Deepak

unread,
Sep 27, 2009, 9:09:39 AM9/27/09
to
Hi,
Can someone provide some pointers on how to implement thread
specific timer in a multi-threaded program.
This is on Linux using pthreads. Any pointers would be appreciated.

David Schwartz

unread,
Sep 27, 2009, 10:59:47 AM9/27/09
to

My advice to you would be not to do that. Associate the timer with
what the thread is doing, not with the thread.

DS

Pascal J. Bourguignon

unread,
Sep 28, 2009, 6:03:25 AM9/28/09
to
David Schwartz <dav...@webmaster.com> writes:

Yes, instead of having thread-specific timers, have any number of
generic timers.

cf. the POSIX.1-2001 functions setitimer(2) and getitimer(2),
or the POSIX.1-2008 functions timer_settime(2) and timer_gettime(2.

--
__Pascal Bourguignon__

Mukul Srivastava

unread,
Sep 29, 2009, 9:00:13 AM9/29/09
to
On Sep 28, 3:03 pm, p...@informatimago.com (Pascal J. Bourguignon)
wrote:

You can store these values in thread specific variable and use it .

Deepak

unread,
Sep 30, 2009, 6:16:23 AM9/30/09
to

I didnt quite understand what you meant by "what the thread is doing"
Vs with the thread. Can you pls elaborate.

David Schwartz

unread,
Sep 30, 2009, 10:55:25 AM9/30/09
to
On Sep 30, 3:16 am, Deepak <dpkshe...@gmail.com> wrote:

> I didnt quite understand what you meant by "what the thread is doing"
> Vs with the thread. Can you pls elaborate.

Okay, let's start with a simple question -- why would anyone want a
thread-specific timer? What possible function could it serve? Whatever
it is that you want to impose a timeout on, why associate the timer
with a thread?

It makes no sense to say "whatever this thread happens to be doing, I
want it to timeout in X time".

It does make sense to say "I need X to happen in Y time, unless Z",
but what does that have to do with any particular thread?

DS

Chris Friesen

unread,
Sep 30, 2009, 11:21:11 AM9/30/09
to
On 09/30/2009 08:55 AM, David Schwartz wrote:

> Okay, let's start with a simple question -- why would anyone want a
> thread-specific timer? What possible function could it serve? Whatever
> it is that you want to impose a timeout on, why associate the timer
> with a thread?
>
> It makes no sense to say "whatever this thread happens to be doing, I
> want it to timeout in X time".

I agree with you that most of the time it wouldn't make sense.

This is a bit of a corner case, but it's possible to set up posix timers
to use various clock sources. If you set the timer to use a per-thread
cpu-time clock, then this allows you to trigger a timer to fire in X
amount of runtime.

This can be useful if what you care about is runtime rather than actual
wall clock time.

Chris

Deepak

unread,
Oct 1, 2009, 8:31:33 AM10/1/09
to

Hi Chris,
Yes i am not interested in wall clock time, but needed a timer for X
amount of runtime, which can be different for different thread of my
app.

>If you set the timer to use a per-thread
>cpu-time clock, then this allows you to trigger a timer to fire in X
>amount of runtime.

So how does one use the per-thread cpu-time clock ?

0 new messages