Class thread Interface Class ithread implementation

Source: Internet
Author: User

 

/*************************************** ************************
* Name: ithread. h
* Function: In-class thread Interface Class
* Programmed: ming_zhang
* Date (org): July 28 2011
* Project: MTL
* OS: Windows XP
* History:
* ID --- date ----------- note -------------------------------
* 00
**************************************** ***********************/

# Ifndef _ ithread_h _
# DEFINE _ ithread_h _

/********************** Global variable declare ************* **/
// # Define-1;
//////////////////////////////////////// //////////////////////

/*********************** Include files ************* ************/
// # Pragma warning (Disable: 4786)
// # Include <iostream>
// # Include <string>
// # Include <map>
// Using namespace STD;
//////////////////////////////////////// ///////////////////////

/*************************************** ************************
* Name: ithread
* Base class name:
* Function:
* Interface:
**************************************** ***********************/
Class ithread
{
Public:
// Constructors & destructor
Ithread ();
Virtual ~ Ithread ();
 
Int thread_create (); // The thread creates a function.
Const cstring & thread_errinfo ()
{
Return m_serrinfo;
}
// Interface
Public:
Protected:
Static unsigned long _ stdcall therad_enter (void * lpvoid); // thread entry function
PRIVATE:
Virtual int thread_main () = 0; // thread processing function
Virtual int thread_befmain (); // pre-processing function of the thread processing function
Virtual int thread_aftmain (); // post-processing function of the thread processing function
Virtual int thread_errmain (); // The error handling function of the thread processing function.

Protected:
Cstring m_serrinfo; // error message
Handle m_hthread; // thread handle
DWORD m_nthreadid; // thread ID
// Attributes
PRIVATE:
// Operations

// Attributes

};

# Endif;

 

/*************************************** ************************
* Name: ithread. cpp
* Function:
* Programmed: ming_zhang
* Date (org): July 28 2011
* Project: MTL
* OS: Windows XP
* History:
* ID --- date ----------- note -------------------------------
* 00
**************************************** ***********************/

/*********************** Include files ************* ************/
# Include "stdafx. H"
# Include "ithread. H"
// # Include <string>
// Using namespace STD;
//////////////////////////////////////// ///////////////////////

Ithread: ithread ()
{
}

Ithread ::~ Ithread ()
{
}
/*************************************** *****************************
* Name: therad_enter
* Function:
* Parameter:
* Return:
* Programmer: Ming Zhang
* Date (org): July 28 2011
* Update:
* Memo:
**************************************** ****************************/
Unsigned long _ stdcall ithread: therad_enter (void * lpvoid)
{
Ithread * pthis = (ithread *) lpvoid;
Int nret = 0;
If (1 = pthis-> thread_befmain () // perform some operations before the thread is executed.
{
If (1 = pthis-> thread_main () // execution thread
Pthis-> thread_aftmain (); // some operations are executed after the thread is successfully executed.
Else
Pthis-> thread_errmain (); // perform some operations after the thread executes an error
}
Return 1;
}
/*************************************** *****************************
* Name: thread_befmain
* Function: provides a processing opportunity to the subclass before thread_main runs.
* Parameter:
* Return: 1: You can run thread_main.0: you cannot run thread_main.
* Programmer: Ming Zhang
* Date (org): July 28 2011
* Update:
* Memo:
**************************************** ****************************/
Int ithread: thread_befmain () // thread processing function
{
Return 1;
}
/*************************************** *****************************
* Name: thread_main
* Function: run the main function in a thread.
* Parameter:
* Return:
* Programmer: Ming Zhang
* Date (org): July 28 2011
* Update:
* Memo:
**************************************** ****************************/
Int ithread: thread_main ()
{
Return 1;
}
/*************************************** *****************************
* Name: thread_aftmain
* Function: perform some operations after the thread runs.
* Parameter:
* Return:
* Programmer: Ming Zhang
* Date (org): July 28 2011
* Update:
* Memo:
**************************************** ****************************/
Int ithread: thread_aftmain ()
{
Return 1;
}
/*************************************** *****************************
* Name: thread_errmain
* Function: error handling function
* Parameter:
* Return:
* Programmer: Ming Zhang
* Date (org): July 28 2011
* Update:
* Memo:
**************************************** ****************************/
Int ithread: thread_errmain ()
{
Return 1;
}
/*************************************** *****************************
* Name: thread_create
* Function: Creates a class thread.
* Parameter:
* Return: 0 failed, 1 successful
* Programmer: Ming Zhang
* Date (org): July 28 2011
* Update:
* Memo:
**************************************** ****************************/
Int ithread: thread_create ()
{
M_hthread = createthread (null, 0, therad_enter, this, null, & m_nthreadid );
If (m_hthread = NULL)
{
M_serrinfo.format ("failed to create thread [% d]! ", Getlasterror ());
Return 0;
}
Return 1;
}

 

 

 

 

 

 

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.