Click to See Complete Forum and Search --> : Resume Thread/Suspend Thread


Alphadan
May 30th, 2011, 11:14 PM
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?

VictorN
May 31st, 2011, 12:57 AM
You may want to look at the Pausing a Thread and Thread Shutdown (http://www.flounder.com/workerthreads.htm#Pausing a Thread) chapter in the Joe Newcomer's essay "Using Worker Threads".

MikeAThon
June 2nd, 2011, 01:33 PM
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

Alphadan
June 3rd, 2011, 05:23 PM
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.

Arjay
June 4th, 2011, 09:23 AM
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.