VC three ways to start a new thread

Source: Internet
Author: User
Tags function prototype
The first type of AfxBeginThread ()

Using the AfxBeginThread () function to create a new thread to perform the task, the worker thread's AfxBeginThread prototype is as follows:
cwinthread* AfxBeginThread (Afx_threadproc Pfnthreadproc,
LPVOID LParam,
int npriority = Thread_priority_normal,
UINT nstacksize = 0,
DWORD dwcreateflags = 0,
Lpsecurity_attributes lpsecurityattrs = NULL
);//used to create worker threads
Return value: Returns a pointer to a thread object that points to a new thread if successful, otherwise null.
Pfnthreadproc: The entry function of the thread, the declaration must be as follows: UINT mythreadfunction (LPVOID pparam), cannot be set to null;
Pparam: Pass in the parameter of the thread, note its type is: LPVOID, so we can pass a struct body into the thread.
Npriority: The priority of the thread, typically set to 0. Let it have the same priority as the primary thread.
Nstacksize: Specifies the size of the stack for the newly created thread. If 0, the newly created thread has the same size stack as the main thread
Dwcreateflags: Specifies how the line Chengyu after the thread is created. You can specify two values:
create_suspended: After the thread is created, it is in a pending state until the call: ResumeThread
0: When the thread is created, it starts running.
Lpsecurityattrs: Point to a security_attributes structure that flags the security of the newly created thread. If NULL,
Then the newly created thread has the same security as the main thread.
If you want to end threads within a thread, you can call AfxEndThread within a thread.

General Direct use AfxBeginThread (threadproc,this);

Example:

UINT  MyProc (lpvoid  lParam)
{
Cittdlg *pwnd = (Cittdlg *) LParam;         Assigns a window pointer to a pwnd->kmeanssegment pointer
();                         The function to execute return
1;
}

void Cittdlg::kmeanssegment ()
{
//main processing function here write
}

void Cittdlg::onkmeanssegment ()             //button click Execute
{

AfxBeginThread (MyProc, (LPVOID) this);//Start a new thread

}

Note that the worker thread's function must be a global function or a static member function, not a normal member function.


The second type of CreateThread ()The function prototype is: HANDLE CreateThread (
NULL,//No security descriptor
0,//default line stacks size
MyThreadProc,//thread function pointer, that is, functions name
(LPVOID) &n,//Pass parameters
NULL,//No attached property
NULL//No need to get the thread number
);
Creatthread, which returns a handle, and closes the thread handle with CloseHandle () if no more threads are needed to monitor the thread.
The function of the thread must be defined as: DWORD winapi MyThreadProc (LPVOID pparameter);

The following shows a multithreaded operation control, clicking a button and then running a thread to display the string inside the CEdit control;
Example:

. h header file

struct HS
    {
        CString Tmp;
        Ctestdlg *hwnd;
    };/ /defines the global structure, which is used to pass the custom message

DWORD WINAPI ThreadProc (Lpvoidlpparam);//thread function declaration, global function public

:
    CString chtmp;
    struct HS    *htmp;

Protected:
   HANDLE m_hthread;//thread handle
   CEdit  m_edit;

. CPP implementation file

//thread execution function

DWORD WINAPI   ThreadProc (LPVOID lpparam)
{
//here writes the processing function
        struct HS *tmp2;
        TMP2 = (hs*) Lpparam;
Operation:
       Tmp2->hwnd->m_edit.setwindowtext ((LPTSTR) tmp2->tmp);
}

void Ctestdlg::onbnclickedbutton1 ()
{
    htmp->tmp = chtmp;
    Htmp->hwnd = this;//key is to pass this pointer in
   m_hthread =createthread (null,0,threadproc,htmp,0,null);//Create New thread
   CloseHandle (M_hthread);
}

Creating a thread with the CreateThread () function returns a thread handle through which you can control and manipulate the thread, and when you do not use it, you can close the handle as soon as you create it, with a special letter CloseHandle (). Closing a handle does not mean closing the thread, except that you cannot control the thread externally (for example, end prematurely, change priority, and so on). When the thread ends, the system automatically cleans the threads resource, but does not automatically close the handle, so remember to close the handle after the thread finishes.



The third type of _beginthread ()
The function prototype is: intptr_t _beginthread (
void (*start_address) (void *),//The starting address of a function called by a new thread
unsigned stack_size,//stack size, set 0 to system defaults
void *arglist//arguments passed to the thread function, no null
);
return value:
If successful, the function will return a handle to a new thread, and the user can declare a handle variable like this to store the return value:
HANDLE hstdout = _beginthread (checkkey, 0, NULL). If the failure _beginthread will return-1. Library file:
#include <process.h>
The definition of the thread function:
For a thread created by _beginthread (), its thread function is defined as:
void Threadpro (void * parguments);

_beginthreadex () is an upgraded version of _beginthread ().


Summary: AfxBeginThread is the global function of MFC, is the encapsulation of CreateThread. CreateThread is the Win32 API function, AfxBeginThread eventually to the CreateThread. And _beginthread is the Run-time library function of C.


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.