To create a simple thread

Source: Internet
Author: User

HANDLE CreateThread (

_in_opt_ lpsecurity_attributes lpthreadattributes,

The //security_attributes structure specifies the security attribute for this thread, and if NULL is created with the default security descriptor, and the returned handle is not inherited.

 

_in_size_t Dwstacksize,

//The stack size of the thread (in bytes), and the default is as large as the main thread if it is 0. The stack is automatically allocated in the memory space of the process and released at the end of the process. If necessary the stack size can be increased.

//createthread attempts to allocate memory with a size of dwatacksize bytes and returns a failure message if the available memory is low.

  

_in_lpthread_start_routine lpstartaddress,

//point to the function provided by the application that the thread needs to execute, which also represents the address at which the thread started. function takes a 32-bit argument and returns a 32-bit value

 

_in_opt_ __drv_aliasesmem lpvoid lpparameter,

//Specify a 32-bit parameter value to pass to the thread

 

_in_dword dwCreationFlags,

//Specify an additional flag to control the creation of the thread. If the create_suspended flag is defined, the thread is created in a pending state until the ResumeThread () function is invoked

//If the value is 0, the thread starts executing immediately after creation

 

_out_opt_ Lpdword Lpthreadid

//point to a id,32 bit variable that holds the thread

            )

         

If the execution succeeds, its return value refers to a handle to the next new thread. If it fails, NULL is returned.

when you finish executing a thread, you should close the handle of the thread. CloseHandle (), which returns a point handle using CreateThread () and adds a reference counter of the corresponding kernel object minus 1.

This is not to force a thread to close, but rather to tell the system to run when the thread is at the end.

BOOL CloseHandle (_in_ HANDLE hobject) returns true successfully, no false

 

test Code 1 when the number of thread loops is less than the number of main threads

 

#include <windows.h> #include <iostream> using namespace std;
    DWORD WINAPI Print_thread (lpvoid data) {cout<< "start thread \ n"; for (int index=0;index<25; index++)//Loop 25 times {cout<< "Thread_" << (int) data<< "_" &LT;&LT;INDEX&L
        t;< "\ n";
    Sleep (100);
    cout<< "Print_thread thread execution complete \ n";
Return (DWORD) data; 
        the int main () {HANDLE threadhandle;//declares a handle DWORD threadid;//thread ID threadhandle=createthread (NULL,
 
    0, Print_thread, (LPVOID) 1, 0, &threadid);
        for (int index=0;index<50; index++)//Loop 50 times {cout<< "start_2_" <<index<< "\ n";
    Sleep (100);
 
    } closehandle (Threadhandle);
    System ("pause");
return 0; Test Code 2 #include <windows.h> #include <iostream> using namespace std DWORD WINAPI print_th When the number of thread loops is greater than the number of main threads
    Read (LPVOID data) {cout<< "start thread \ n"; for (int index=0; index<50;index++)//Loop 50 times {cout<< "Thread_" << (int) data<< "_" <<index<< "\ n";
    Sleep (100);
    cout<< "Print_thread thread execution complete \ n";
Return (DWORD) data; the int main () {HANDLE threadhandle;//declares a handle DWORD threadid;//thread ID threadhandle=createthread (NUL
 
    L, 0, Print_thread, (LPVOID) 1, 0, &threadid);
        for (int index=0; index<25;index++)/Loop 25 times {cout<< "start_2_" <<index<< "\ n";
    Sleep (100);
 
    } closehandle (Threadhandle);
    System ("pause");
return 0; }


just looking at code 1 may not be a special discovery, so I wrote code 2 to compare it to. Attentive readers will find that I just changed the number of main thread and child threads for loop.

compare two codes and you'll find that when the main thread is finished, the child thread continues, because I added system ("pause"), so the program does not exit directly after the for loop of the first main thread is finished, but we can see that the child thread is still running until the end is stopped. If you will system ("Pause"), you will find that when the main thread is finished, the program exits directly and does not wait for the child thread to end.

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.