Re: Can I implement a timer within a thread without a blocking wait?
- From: kunniu <haoniukun@xxxxxxxxx>
- Date: 2 May 2007 19:40:50 -0700
Appreciate your solution David. :)
How about using poll in each separate thread?
I think that a periodically running process may consume more server
resource. Right?
On 5月3日, 上午5时16分, David Schwartz <dav...@xxxxxxxxxxxxx> wrote:
On Apr 30, 11:59 pm, kunniu <haoniu...@xxxxxxxxx> wrote:
The problem troubles me for a long time.
I'm trying to implement a multithread server program on Linux.
My kernel version is 2.6.17.
My main objective is to wait for a period of time and if it times out,
I'll close the connection.
I used setitimver in my thread.
But I can't implement timer signal handler for each thread.
Do I have to use poll in each thread?
You don't want to time out a thread, you want to time out a
connection. Associate timers with connections, not with threads. You
are making this more difficult than it needs to be by focusing on how
the work is done rather than what work is done.
A typical solution would be:
1) Have a linked-list of timers sorted in expiration order protected
by a mutex.
2) Have a thread that checks the timer list and sees if the head timer
is ready to expire. If so, it calls the timer's expiration function.
3) When you want to timeout a connection, add a timer to the linked
list and let the timer thread do the rest of the work.
A more specific solution to your problem would be as follows:
1) Have a 'timing out' flag for each connection. Have it start out
clear. Also clear the flag anytime the connection is active.
2) Have a service thread that goes through every connection every half
timeout interval (so if the timeout is six minutes, have this thread
run every three minutes). If a connection's flag is clear, this sets
it. If it is set, it has timed out, and the connection can be
shutdown. (It cannot be closed because it may be in use by another
thread, however, if you know it's not in use, you can 'close' it
here.)
DS
.
- Follow-Ups:
- Re: Can I implement a timer within a thread without a blocking wait?
- From: David Schwartz
- Re: Can I implement a timer within a thread without a blocking wait?
- References:
- Can I implement a timer within a thread without a blocking wait?
- From: kunniu
- Re: Can I implement a timer within a thread without a blocking wait?
- From: David Schwartz
- Can I implement a timer within a thread without a blocking wait?
- Prev by Date: Re: Boolean Buyers Beware ... AIX compiler bug --- PMR 26241,756
- Next by Date: Re: Resolution...
- Previous by thread: Re: Can I implement a timer within a thread without a blocking wait?
- Next by thread: Re: Can I implement a timer within a thread without a blocking wait?
- Index(es):
Relevant Pages
|