|
-
May 30th, 2011, 11:14 PM
#1
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.
-
May 31st, 2011, 12:57 AM
#2
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
-
June 2nd, 2011, 01:33 PM
#3
Re: Resume Thread/Suspend Thread
 Originally Posted by Alphadan
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
-
June 3rd, 2011, 05:23 PM
#4
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.
-
June 4th, 2011, 09:23 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|