Introduction and use of the Windows multithreaded interface

Source: Internet
Author: User

A Windows multithreaded interface:

1 Creating Threads

CreateThread and _beginthreadex can be implemented to create threads, parameters of two functions

Same

Handlewinapicreatethread (Lpsecurity_attributeslpthreadattributes, size_tdwstacksize,  LPTHREAD_START_ Routinelpstartaddress, Lpvoidlpparameter, Dworddwcreationflags, Lpdwordlpthreadid);

Function Description:

The first parameter represents the security properties of the thread kernel object, and a general pass-through of NULL means that the default setting is used.

The second parameter represents the size of the line stacks space. Passing in 0 means using the default size (1MB).

The third parameter represents the address of the thread function that the new thread executes, and multiple threads can use the same function address.

The fourth parameter is a parameter passed to the thread function.

The fifth parameter specifies an additional flag to control the creation of the thread, 0 indicates that the thread is ready to be dispatched immediately after it is created, and if create_suspended indicates that the thread is created and paused, it cannot be dispatched until ResumeThread () is called.

The sixth parameter returns the ID number of the thread, and passing in null means that the thread ID number is not required to be returned.

function return value:

A handle to the new thread was successfully returned, and Null was returned for failure.

The difference between CreateThread and _beginthreadex is that _beginthreadex is more secure, and _beginthreadex allocates separate blocks of data for each thread, a separate block of data that holds the thread's unique information.

Because when calling the standard library of C, some functions are returning the global information, this global information is easy to be interfered by multithreading, _beginthreadex will avoid this problem.

2 thread waits for function WaitForSingleObject,

WaitForSingleObject This function causes the thread to wait for a particular object, allowing the thread to enter a wait state until the specified kernel object is triggered.

Dwordwinapiwaitforsingleobject (Handlehhandle, dworddwmilliseconds);

Function Description:

The first parameter is the kernel object to wait for.

The second parameter is the longest waiting time, in milliseconds, such as passing in 5000 for 5 seconds, passing in 0 and returning immediately, and passing in infinite means infinite wait.

Because the thread's handle is not triggered when the thread runs, the threads end up running and the handle is in the triggered state. So you can use WaitForSingleObject () to wait for a thread to end up running.

function return value:

The object is triggered at the specified time, and the function returns WAIT_OBJECT_0. The maximum wait time object has not yet been triggered to return wait_timeout. An error in the incoming parameter will return wait_failed

The specific use

I can intercept some of the code from my own server.

voidBasethread::startup (UInt32 stackSize) {assert (M_nid==0); #ifDefined _win32M_hthread= (HANDLE) _beginthreadex (NULL,0, ThreadFunc, This,0, &M_nid); //cout << this <<endl;:: SetThreadPriority (:: GetCurrentThread (),2); //let the thread run and then exit the function.//Sleep (+);        #endif}

voidBasethread::join () {#ifDefined _win32DWORD ExitCode;  while(1)     {        if(GetExitCodeThread (M_hthread, &exitcode)! =0)        {            if(ExitCode! =still_active) {                 Break; }            Else            {                //before wait, you need to invoke the thread to prevent the thread from being suspended causing the deathResumeThread (M_hthread);            WaitForSingleObject (M_hthread, INFINITE); }        }        Else        {             Break;    }} CloseHandle (M_hthread); #endifM_nid=0;}

My public number, thank you for your attention:

Introduction and use of the Windows multithreaded interface

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.