Multithreading 2: multithreading in Windows-Basics

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.