The Code is as follows:
// Thread. cpp: Win32 thread library, which can only be demonstrated at present
// Author: galphy-6-20
// Recommended ide: Dev-C ++, based on G ++, lightweight
//////////////////////////////////////// //////////////////////////////////
# Include <stdio. h>
# Include <windows. h>
Typedef unsigned long (_ stdcall * threadfunc) (void *);
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 (threadfunc func, void * Arg)
{
M_threadid = 0;
M_handle = NULL;
Exitcode = 0;
M_handle = createthread (0, 0, func, ARG, create_suincluded, & m_threadid );
}
/**
* Start the thread
*/
DWORD resume ()
{
If (m_handle)
{
Return resumethread (m_handle );
}
}
/**
* Thread Suspension
*/
DWORD suspend ()
{
If (m_handle)
{
Return suspendthread (m_handle );
}
}
/**
* 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;
};
/**
* Callback function of the thread
*/
Unsigned long do_work (void * P)
{
For (INT I = 0; I <1000000; I ++)
{
Printf ("% D ----> % d/N", I, (int *) P );
// Sleep (1, 1000 );
}
Return 0;
}
// Test the main program
Int main (INT argc, char * argv [])
{
Thread mythread (threadfunc) do_work, (void *) 1000 );
Mythread. Resume ();
Sleep (1000 );
Mythread. Suspend ();
Sleep (1000 );
Mythread. Resume ();
Thread mythread1 (threadfunc) do_work, (void *) 1000 );
Mythread1.resume ();
Thread mythread2 (threadfunc) do_work, (void *) 1000 );
Mythread2.resume ();
Sleep (5000 );
Return 0;
}