how to implement per-thread timer in a multi-thread program?

L

liu yang

I want to build a timer for each thread, and the timer must be thread-
safe. How to do that?

My environment is Linux and Pthread.

Thanks.
 
N

Nick Keighley

I want to build a timer for each thread, and the timer must be thread-
safe. How to do that?

My environment is Linux and Pthread.

comp.lang.c is for the discussion of standard C (as defined by the ISO
standard). Consequently it does not discuss timers or threads. You
ned to look in a thread or platform (OS) specific group for more
information.
 
K

Keith Thompson

liu yang said:
I want to build a timer for each thread, and the timer must be thread-
safe. How to do that?

My environment is Linux and Pthread.

Try a Linux newsgroup or comp.programming.threads.
 
A

Antoninus Twink

I want to build a timer for each thread, and the timer must be thread-
safe. How to do that?

My environment is Linux and Pthread.

Unfortunately, there is no way to get POSIX alarms to signal a calling
thread specifically. So you'll need to do some manual coding around it.

You can set up a thread to deal with the alarms. It blocks all signals
except that it sets up a handler for SIGALRM, then enters a sigwait()
loop.

When another thread wants to receive that alarm, it sets up a handler
for SIGUSR1 (say), and calls a function you provide that registers that
thread with your waiting thread.

When the process is sent a SIGALRM, the waiting thread returns from
sigwait(), works out which thread it should send the alarm to, and sends
a SIGUSR1 to that thread.

Of course you need to be quite careful to keep track of which thread
asked for an alarm at what time, and reset the alarm when necessary.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top