: > using > 2 threads. Each of them needs to use a timer for a
: > specific task. When I set a timer in one thread using alarm(),
: > some other thread also sets the alarm() and overrides this. How
: > do I use thread level timer apart from using mutexs before alarm() ?
: Instead of using timers, use cond_timedwait(). cond_timedwait() is
: MT-Safe and has a much finer resolution than alarm().
:
: see man condition for further explanation of the condition variable.
: --
: =========================================
: Dan Dunn
: =========================================
Dan,
thanks for the suggestion. However, I had mis-stated the problem in
the subject.
What I really need is some means of detecting a timeout in the same
thread. For example, I have a thread that is attempting a TCP
connect() to a remote machine. If the remote machine is down, then
connect would take nearly 3 minutes to timeout. To overcome this I
was trying something like this:
try {
TIMER connect_timer(duration);
connect(...);
}
catch(..) {
/* couldn't connect in 'duration' seconds ==> host/network is down */
..
}
The TIMER object sets the alarm(duration) and a SIGALRM handler that
throws an exception on SIGALRM.
This works ok on a single threaded environment but obviously not
in a multi-threaded program because different threads may be
connecting to different machines.
Is there an alternate way of doing this without using multiple proceses ?
thanks for any pointers,
-ramesh
____________________________________________________
Ramesh Nagabushnam, (o) (914) 644-2711