-
January 18th, 2012, 05:13 PM
#1
[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!
Last edited by vincegata; January 19th, 2012 at 03:30 PM.
-
January 18th, 2012, 05:47 PM
#2
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).
-
January 18th, 2012, 06:59 PM
#3
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.
-
January 18th, 2012, 07:20 PM
#4
Re: How can I restart a thread at midnight?
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?
Last edited by vincegata; January 19th, 2012 at 02:42 PM.
-
January 19th, 2012, 02:36 PM
#5
Re: How can I restart a thread at midnight?
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.
-
January 19th, 2012, 02:44 PM
#6
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.
-
January 19th, 2012, 03:26 PM
#7
Re: How can I restart a thread at midnight?
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.
-
January 19th, 2012, 03:29 PM
#8
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.
-
January 19th, 2012, 04:04 PM
#9
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
-
January 19th, 2012, 04:40 PM
#10
Re: How can I restart a thread at midnight?
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.
-
January 19th, 2012, 05:19 PM
#11
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
-
January 19th, 2012, 06:49 PM
#12
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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|