Chapter: Multicore Application Programming For Windows, Linux, and Oracle Solaris - Windows Threading

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail

Creating and Resuming Suspended Threads

A suspended thread is one that is not currently running. Threads can be created in the suspended state and then started at a later time.

Creating and Resuming Suspended Threads

 

A suspended thread is one that is not currently running. Threads can be created in the suspended state and then started at a later time. If a thread is in the suspended state, then the call to start the thread executing is ResumeThread(), which takes the handle of the thread as a parameter. There is a SuspendThread() call that will cause a running thread to be suspended. This call is expected to be used only by tools such as debuggers; sus-pending a running thread may lead to problems if the thread currently holds resources such as mutexes. Listing 6.10 shows the creation of a suspended thread and then calling ResumeThread() on that thread. The code uses a call to getchar(), which waits for the enter key to be pressed, to separate the creation of the thread from the act of resum-ing the thread.

 

Listing 6.10   Creating and Resuming a Suspended Thread

#include <windows.h> #include <process.h>

 

unsigned int __stdcall mywork( void * data )

 

{

 

printf( "Thread %i\n", GetCurrentThreadId() ); return 0;

 

}

 

 

int _tmain( int argc, _TCHAR* argv[] )

 

{

 

HANDLE handle;

 

handle = (HANDLE)_beginthreadex(0,0, &mywork, 0, CREATE_SUSPENDED, 0);

 

getchar();

 

ResumeThread( handle );

 

getchar();

 

WaitForSingleObject( handle, INFINITE );

 

CloseHandle( handle );

 

return 0;

 

}

 

The suspension state of the thread is handled as a counter, so multiple calls to SuspendThread() need to be matched with multiple calls to ResumeThread().


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.