CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2009
    Posts
    252

    Resume Thread/Suspend Thread

    Im implementing the re entrance on my application i start threads to process some data then when the job finishes the thread gets on hold for 15 seconds if there is another job avaliable it will asign it to that thread Resuming it, But if there are no more jobs and the 15 seconds pass the thread exits.

    sometimes i try to resume threads but they wont start i improved my error handler and i been monitoring the suspend count i noticed these threads that wont start are returning 0 instead of 1 like they should.

    the documentation says the thread was not suspended, but doesnt say anything else im not sure if the thread was really ruuning or even if it was terminated by exiting its entrypoint function...

    so my question is would Resume Thread return 0 if the thread is not suspended but ended? or would it return -1?

    how would i track that bug i have no idea yet, the thread should be suspended everytime it finishes its job for 15 seconds if there is not another job in the next 15 seconds the thread exits normally.

    any recomendation is welcome thx in advance!

    Edit: Is this way to add pauses using resume thread and and suspend thread a good practice?
    Last edited by Alphadan; May 31st, 2011 at 12:30 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Resume Thread/Suspend Thread

    You may want to look at the Pausing a Thread and Thread Shutdown chapter in the Joe Newcomer's essay "Using Worker Threads".
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Resume Thread/Suspend Thread

    Quote Originally Posted by Alphadan View Post
    Edit: Is this way to add pauses using resume thread and and suspend thread a good practice?
    No, IMO it's not good practice, and I think that many here would agree that you should avoid use of SuspendThread and ResumeThread as a synchronization architecture between threads.

    Some reasons are given in the article linked by VictorN.

    Other reasons are found in the documentation for these functions, which takes great pains to point out that these functions should only be used if you are programming a debugger (like Visual Studio is).

    For a synchronization architecture between threads, use mutexes and WaitForSingleObject (or WaitForMultipleObjects), which is also explained in the linked article from VictorN.

    Mike

  4. #4
    Join Date
    Feb 2009
    Posts
    252

    Re: Resume Thread/Suspend Thread

    Thx you both im gonna listen you and im gonna rewrite that part, i actually tought it was elegant to suspend the thread when was not in use and then resume it when needed hehehe such a shame it is a bad practice.

    Thx again.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Resume Thread/Suspend Thread

    You can still have the functionality, just don't use Suspend/Resume thread.

    If you want to do this, just create a while loop in the thread and call waitformultipleobjects inside the while loop. Create two events: shutdownevent and doworkevent and have the wfmo function wait on these events.

    If the shutdownevent is set, return from the thread. If the doworkevent is set, do the work, then reset the doworkevent.

    When the work has been completed, execution will go to the top of the while and the thread will be put to sleep while waiting for the wfmo call.

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