|
-
July 28th, 2005, 06:01 AM
#1
Timers and Critical Section - Best practices
I have a service which executes a lengthy process once a day. A timer executes every minute and performs a check to see if this process is due to be run. Only after the process completes successfullly does it mark a variable such that subsequent timers simply return.
So the question is, what is the best way to ensure this lengthy process is called only once. If it takes 3 minutes to complete and the timer is running once a minute, up to three calls to the lengthy process could be made.
The lengthy process needs only to be run once, thus its not a matter of using typical thread synchronization where the other timers need to wait for the first to return. Essentially a boolean can be kept to mark the lengthy process as running. Calls to the lengthy process could simply return if this boolean is flagged.
The real question is, what is the best way to accomplishing this?
PS: this is more a question out of professional curiousity rather than something I'm stuck on. I'm just curious about the best practice for this scenario. In reality I decided to mark the variable whether the process fails or succeeds.
Cheers,
Chris
-
July 28th, 2005, 07:36 AM
#2
Re: Timers and Critical Section - Best practices
Personally I'd suspend the timer before executing the process and resume it afterwards.
Darwen.
-
July 28th, 2005, 08:04 AM
#3
Re: Timers and Critical Section - Best practices
Cheers darwen, as I said its not a matter of suspending timers or setting a boolean until the operation is complete. The question is, relating to best practices for a situation where you have a critical section but do not want other threads to wait.
Essentially the situation boils down to a winning thread locks the critical section, and all other threads aborting. What is the best way to do this?
-
July 28th, 2005, 10:12 AM
#4
Re: Timers and Critical Section - Best practices
Personally I'd use an event like this one, with a zero wait timeout period :
Code:
static private AutoResetEvent m_event = new AutoResetEvent(true);
private ThreadFunc()
{
if (m_event.WaitOne(0, false))
{
// only one thread will ever get to here at a time
// the others will fall through and exit
m_event.Set();
}
}
You could do the same thing with a Mutex object too of course.
Darwen.
-
July 28th, 2005, 10:29 AM
#5
Re: Timers and Critical Section - Best practices
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
|