The Code is as follows:
// Thread. cpp: Win32 thread library, which can only be demonstrated at present
// Author: galphy-6-20
//////////////////////////////////////// //////////////////////////////////
# Include <windows. h>
# Include <stdio. h>
Typedef unsigned long (_ stdcall * threadfunc) (void *);
Class runnable {
Public:
/**
* Runnables shocould never throw in their Destructors
*/
Virtual ~ Runnable (){}
/**
* Task to be executed med in another thread of execution
*/
Virtual void run () = 0;
Friend threadfunc do_work (runnable * Run );
};
/**
* Callback function of the thread
*/
Namespace threadwork {
Threadfunc do_work (runnable * runnable)
{
If (runnable)
Runnable-> Run ();
Return 0;
}
}
Class thread
{
Public:
/**
*
*/
// Void exit ()
//{
// If (true)
//{
// Exitthread (exitcode );
//}
//}
/**
*
*/
// Void stop ()
//{
// DWORD status;
// Int I;
// For (I = 0; I <200; I ++)
//{
// If (: getexitcodethread (m_handle, & Status) & status = still_active) sleep (10 );
// Else break;
//}
// If (I> = 200) Kill (); // kill it if it does not stop for 2 seconds.
//}
/**
* Killing threads
*/
Bool kill ()
{
If (m_handle)
{
Return terminatethread (m_handle, exitcode );
}
}
/**
* Create thread
*/
Thread (runnable * runnable)
{
M_threadid = 0;
M_handle = NULL;
Exitcode = 0;
M_handle = createthread (0, 0, (threadfunc) threadwork: do_work, (void *) runnable, create_suincluded, & m_threadid );
}
/**
* Start the thread
*/
DWORD resume ()
{
If (m_handle)
{
Return resumethread (m_handle );
}
Else
{
Return 0;
}
}
/**
* Thread Suspension
*/
DWORD suspend ()
{
If (m_handle)
{
Return suspendthread (m_handle );
}
Else
{
Return 0;
}
}
/**
* Clear threads
*/
~ Thread ()
{
If (m_handle)
{
Closehandle (m_handle );
}
M_threadid = 0;
M_handle = NULL;
Exitcode = 0;
}
PRIVATE:
DWORD m_threadid;
Handle m_handle;
DWORD exitcode;
};
Class test: Public runnable
{
Public:
Test (){};
~ Test (){};
Void run ()
{
For (INT I = 0; I <10000000; I ++)
{
Printf ("% d -------------/N", I );
}
}
PRIVATE:
Int;
};
// Test the main program
Int main (INT argc, char * argv [])
{
Thread mythread (new test ());
Mythread. Resume ();
Sleep (1000 );
Mythread. Suspend ();
Sleep (1000 );
Return 0;
}