[RESOLVED] How can I restart a thread at midnight?
[EDITED]
Hello,
I am running Ubuntu 11.04.
I have an application that downloads data from the remote web server. This web server shuts down for maintenance at midnight for about ten minutes. So I need to disconnect from the server at 11:55pm and connect again at 12:05am. I use sockets so to disconnect I need to logout and close the socket, and to re-connect I need to open socket and logon. The part of my application that downloads data runs in its own thread.
So I need to stop the thread at 11:55pm and start at 12:05am. How can I do that in non-blocking way? I do not want to run a loop and poll for system time that will suck up CPU time.
TIA!
Re: How can I restart a thread at midnight?
The typical approach in this situation has nothing to do with the actual time; it just detects when the server is no longer connected (typically using a timeout), and then keeps trying to reconnect until it succeeds.
In order to avoid having these reconnection attempts use too many resources, it is common to double the amount of wait time after each failed connection attempt, up to some reasonable maximum (perhaps 2 minutes).
Re: How can I restart a thread at midnight?
If it were me, I'd set a timer to go off every minute or so and check the system time. Overhead would be minimal.
Re: How can I restart a thread at midnight?
Quote:
Originally Posted by
GCDEF
If it were me, I'd set a timer to go off every minute
How exactly do I do that? Run sleep(60) in separate thread?
Re: How can I restart a thread at midnight?
Quote:
Originally Posted by
Lindley
The typical approach in this situation has nothing to do with the actual time; it just detects when the server is no longer connected (typically using a timeout), and then keeps trying to reconnect until it succeeds.
I need this too to make my app robust - to reconnect if the server unexpectedly drops the connection. I do not know how to check if the server is no longer connected. I'll post this question in sockets sub-forum.
Thank you.
Re: How can I restart a thread at midnight?
If it's a TCP socket, it has internal means to validate that the connection is still active. If the connection goes down, you typically get an error from any function you call tell you so (send, recv, etc).
If you're using UDP you will need to write the keepalive logic yourself.
Re: How can I restart a thread at midnight?
Quote:
Originally Posted by
Lindley
If it's a TCP socket, it has internal means to validate that the connection is still active. If the connection goes down, you typically get an error from any function you call tell you so (send, recv, etc).
I see, thank you. It's TCP.
Re: How can I restart a thread at midnight?
I am gonna use setitimer, or sleep(60) in separate thread. I'll see what's better/easier.
Thank you for suggestions.
SOLVED.
Re: How can I restart a thread at midnight?
>> I'll see what's better/easier.
The better/easier approach was given in post #2. You don't need timers or sleeps.
gg
Re: How can I restart a thread at midnight?
Quote:
Originally Posted by
Codeplug
The better/easier approach was given in post #2. You don't need timers or sleeps.
gg
You are probably right, I need my app to be able to recover from connection loss anyway.
Re: How can I restart a thread at midnight?
>> I need my app to be able to recover from connection loss anyway.
At least you won't have to change to clock to test your recover code - just pull the plug :)
gg
Re: How can I restart a thread at midnight?
>> At least you won't have to change to clock to test your recover code - just pull the plug :)
that's also true :)