Windows API Multithreading

Source: Internet
Author: User

A thread is a single sequential control flow in a program. Running multiple threads at the same time in a single program accomplishes different tasks, called multithreading.

The above from Baidu Encyclopedia. Multithreaded technology makes programs more powerful, and is a single thread that can never be implemented. For example, at the command line, the user is responding to the GetChar function, and I need to change the command-line header to the current time when the user enters it, which is a single thread that is never possible to achieve. Learning the content of this tutorial, the above example is not difficult to do.

First, recognize the API function CreateThread:

HANDLE CreateThread (
Lpsecurity_attributes Lpthreadattributes,
DWORD Dwstacksize,
Lpthread_start_routine Lpstartaddress,
LPVOID Lpparameter,
DWORD dwCreationFlags,
Lpdword Lpthreadid
);

Parameters are complex, but in fact most of the parameters in normal programming are not needed. Look directly at the sample code:

void Thread1 ()
{
while (1)

{

System ("title%time%");

}
}

void Main (int argc, char* argv[])
{
HANDLE Hthread;
DWORD ThreadID;
Hthread=createthread (null,0, (lpthread_start_routine) thread1,null,0,&threadid);
GetChar ();
}

(if prompted GetChar not defined, please add stdio.h header file)

Did you achieve the desired effect in our example? Let's examine the code.

First, using the API function in the main function, CreateThread creates a thread with the content of the thread executing the THREAD1 function (parameter three) and then waiting for the user's input to be completed in the GetChar function. At the same time, the thread you just created is executing the contents of the Thread1 function, which is to constantly set the command-line header to the current time. So, our effect was reached.

CreateThread returns the handle of the thread, which is stored in the Hthread variable in the sample code. CreateThread sets the fifth parameter to the ID of the thread. The handle and ID of a thread are important parameters that are important to the other operations of the thread in the future.

-----------------------------------------------------------------------Split Line----------------------------------------------- -----------------------------------------------------------------------

CreateThread parameter Description:

HANDLE CreateThread (
Lpsecurity_attributes lpthreadattributes,//[in, optional] handle can be inherited, null cannot be inherited
DWORD dwstacksize,//stack initial size, such as automatic allocation for 0 system
Lpthread_start_routine lpstartaddress,//pointers to application-defined functions executed by the thread.
LPVOID Lpparameter,//pointer to the variable to pass to the thread.
DWORD dwcreationflags,//flag that controls thread creation. 0 The thread is run immediately after creation, and other optional flags are shown below.
Lpdword Lpthreadid//Pointer to the variable that receives the thread identifier. If this parameter is NULL, the thread identifier is not returned.
);

DWORD dwcreationflags//optional flag

    • 0 The thread runs immediately after it is created.
    • Create_suspended (0x00000004) The thread is created in a suspended state and will not run until the ResumeThread function is called.
    • Stack_size_param_is_a_reservation (0x00010000) specifies the initial reserve size of the stack in the Dwstacksize parameter. If this flag is not specified, dwstacksize specifies the commit size.

-----------------------------------------------------------------------Split Line----------------------------------------------- -----------------------------------------------------------------------

Then, to introduce several API functions, these functions are relatively simple, please let the reader freely test:

DWORD SuspendThread (
HANDLE Hthread
);

This is an API function for suspending threads, which is used to suspend execution of threads. The handle parameter is the thread that is returned when the thread is created.

DWORD ResumeThread (
HANDLE Hthread
);

This is an API function that continues to execute threads, which resumes execution of a thread that has already been suspended, corresponding to the SuspendThread. The handle parameter is the thread that is returned when the thread is created.

VOID ExitThread (
DWORD Dwexitcode
);

This is the API function to stop (destroy) the thread, which can only be used internally. The parameter is the exit code, generally can be set to 0.

BOOL TerminateThread (
HANDLE Hthread,
DWORD Dwexitcode
);

This is a force stop (destroy) thread API function, can be used in any part of the program. However, using this API function to terminate threads is unsafe and can cause program instability. It is not recommended. Parameter one is the thread handle that is returned when the thread is created, and parameter two is the exit code, which is usually set to 0.

Windows API Multithreading

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.