Re: Can I implement a timer within a thread without a blocking wait?



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


.



Relevant Pages

  • Re: Multi-Thread vs Win98
    ... In the client application, I have a Timer ... that "readln" from the server, to know when the application must be ... Perhaps the connection is just timing out on you and dropping the ...
    (borland.public.delphi.non-technical)
  • Re: a Timer in a URLConnection
    ... >> I would like to make a Timer in order to stop the connection if there ... >> is no response from the server in a given amount of time. ...
    (comp.lang.java.programmer)
  • Re: a Timer in a URLConnection
    ... > I would like to make a Timer in order to stop the connection if there ... > is no response from the server in a given amount of time. ...
    (comp.lang.java.programmer)
  • Re: RAS connection and Socket status
    ... You don't need a timer to check the current RAS status. ... recv) to ensure that status messages can be processed. ... RasGetConnectStatus to check it during the connection process ...
    (microsoft.public.pocketpc.developer)
  • Re: Can I implement a timer within a thread without a blocking wait?
    ... I'll close the connection. ... But I can't implement timer signal handler for each thread. ... When you want to timeout a connection, add a timer to the linked ... Also clear the flag anytime the connection is active. ...
    (comp.programming.threads)