Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would I want to start a thread "suspended"?

The Windows and Solaris thread APIs both allow a thread to be created in a "suspended" state. The thread only actually starts when it is later "resumed". I'm used to POSIX threads which don't have this concept, and I'm struggling to understand the motivation for it. Can anyone suggest why it would be useful to create a "suspended" thread?

Here's a simple illustrative example. WinAPI allows me to do this:

t = CreateThread(NULL,0,func,NULL,CREATE_SUSPENDED,NULL);
// A. Thread not running, so do... something here?
ResumeThread(t);
// B. Thread running, so do something else.

The (simpler) POSIX equivalent appears to be:

// A. Thread not running, so do... something here?
pthread_create(&t,NULL,func,NULL);
// B. Thread running, so do something else.

Does anyone have any real-world examples where they've been able to do something at point A (between CreateThread & ResumeThread) which would have been difficult on POSIX?

like image 314
alex tingle Avatar asked Jul 01 '10 10:07

alex tingle


People also ask

What does suspending a thread do?

While a thread is suspended, it is not scheduled for time on the processor. If a thread is created in a suspended state (with the CREATE_SUSPENDED flag), it does not begin to execute until another thread calls the ResumeThread function with a handle to the suspended thread.

What can I use instead of thread suspension?

Suspend has been deprecated. Use other classes in System. Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources.

How do you pause a thread in Java?

Methods Used: sleep(time): This is a method used to sleep the thread for some milliseconds time. suspend(): This is a method used to suspend the thread. The thread will remain suspended and won't perform its tasks until it is resumed. resume(): This is a method used to resume the suspended thread.


2 Answers

  1. To preallocate resources and later start the thread almost immediately.
  2. You have a mechanism that reuses a thread (resumes it), but you don't have actually a thread to reuse and you must create one.
like image 152
adf88 Avatar answered Oct 04 '22 00:10

adf88


It can be useful to create a thread in a suspended state in many instances (I find) - you may wish to get the handle to the thread and set some of it's properties before allowing it to start using the resources you're setting up for it.

Starting is suspended is much safer than starting it and then suspending it - you have no idea how far it's got or what it's doing.

Another example might be for when you want to use a thread pool - you create the necessary threads up front, suspended, and then when a request comes in, pick one of the threads, set the thread information for the task, and then set it as schedulable.

I dare say there are ways around not having CREATE_SUSPENDED, but it certainly has its uses.

There are some example of uses in 'Windows via C/C++' (Richter/Nasarre) if you want lots of detail!

like image 42
Ragster Avatar answered Oct 03 '22 22:10

Ragster