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



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: Can I implement a timer within a thread without a blocking wait?
    ... I think that a periodically running process may consume more server ... I'm trying to implement a multithread server program on Linux. ... I'll close the connection. ... But I can't implement timer signal handler for each thread. ...
    (comp.programming.threads)
  • 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: IBM 4247 printing problem
    ... My suggestion is to change the PSFCFG release timer value from *NOMAX ... The reason is that the connection from your IBM server to the printer ... so long as the print writer is started. ...
    (comp.sys.ibm.as400.misc)
  • Re: Close Reader or not?
    ... Would this be for both the Reader and the Connection? ... My problem is that I am running this from a Windows Service with a timer ... Poller never finishes. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Timeout for AsyncSocket
    ... Set a timer handler. ... check a flag that says whether or not ... When the timer fires, ... >For the Connection I used an Asyncsocket and I'm using OnConnect and ...
    (microsoft.public.vc.mfc)