CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 1999
    Location
    Galway, Ireland
    Posts
    96

    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

  2. #2
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: Timers and Critical Section - Best practices

    Personally I'd suspend the timer before executing the process and resume it afterwards.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  3. #3
    Join Date
    Apr 1999
    Location
    Galway, Ireland
    Posts
    96

    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?

  4. #4
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    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.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  5. #5
    Join Date
    Apr 1999
    Location
    Galway, Ireland
    Posts
    96

    Re: Timers and Critical Section - Best practices

    Nicely done. Cheers!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured