Sign in to follow this  

thread suspended?

This topic is 3630 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

is there any way to determine if a thread (win32) is in a suspended state? I've called SuspendThread(mThreadHandle); on the thread, but i then need to know if a thread is then suspended.

Share this post


Link to post
Share on other sites
From the SuspendThread docs:
Quote:
Return Value
If the function succeeds, the return value is the thread's previous suspend count; otherwise, it is (DWORD) -1.
So, you could call SuspendThread(), and if it returns nonzero, call ResumeThread(). There's no IsThreadSuspended() function or equivalent that I know of. It's also worth noting that if you're doing this from two other threads, you could end up with a problem (Both threads suspend target thread, then both resume it)

Share this post


Link to post
Share on other sites
The problem with a theoretical "IsThreadSuspended" function is that the return value is intrinsically unreliable, since by the time you use the return value from the function the actual thread state could have changed. The SuspendThread() documentation says specifically that "[SuspendThread()] is not intended to be used for thread synchronization".

Share this post


Link to post
Share on other sites
guess i'll explain my problem further then.

i am trying to write some multithread code where i start a number of threads and just put them to sleep. Then when i want to use them i wake them up, they will then put them selves to sleep once they have completed any tasks.

The reason i want to do this is creating threads is very expensive so i thought suspending and resuming them should be significantly cheaper, how ever i need to know if a thread has finished its task.

when a thread suspends it self, the return value never gets set because the thread has gone to sleep :(

Share this post


Link to post
Share on other sites
Quote:
The reason i want to do this is creating threads is very expensive so i thought suspending and resuming them should be significantly cheaper, how ever i need to know if a thread has finished its task.


Why don't you just set a flag before supsending the thread to let your main application know it's available?

m_isAvailable = true;
SuspendThread(m_handle);

Or even better have a set of available threads and then let the thread re-insert themself into the set before suspending:

Lock(m_main->m_setLock);
m_main->m_availThreads.insert(this);
Unlock(m_main->m_setLock);
SuspendThread(m_handle);

Or even better use a lock free queue or other data structure.

Share this post


Link to post
Share on other sites
Do you need tasks to run on a specific thread? If not use an auto-reset event and have them all wait on it whilst not busy. when you have a new task call SetEvent and a thread will activate.

If you need them to have separate synchronisation, you could give each one it's own event. Have the thread reset the event then wait on it when it's finished. To test if a thread is idle do WaitForSingleObject with a 0 timeout - if it times out the thread is idle. Then you can set the event to start the thread off again.

Share this post


Link to post
Share on other sites
okay what i'm trying now is to use a semaphore, i create n threads, and a semaphore with count of n. Then i have a loop all threads run which will block to aquire a sempahore, if they aquire a semaphore, a task is guaranteed to exist, so they will grab one from the end of the task queue.
Now the problem is i want to be able to know when all (or if any) threads are blocked by the semaphore, i will then know its free. There are times when i want to wait till all tasks are completed before adding any more for example.

Share this post


Link to post
Share on other sites

This topic is 3630 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this