Windows multi-thread programming

Source: Internet
Author: User

We know that to create a thread, there must be a main process, and then the main process creates a thread. In general VC programs, the process where the main function is located is the main process of the program.

Let's start with the main function to compile our simple applet. We know that the createthread function can be used to create a thread. In msdn, find this function and obtain the following information: "The createthread function creates a thread to execute within the address space of the calling process. "and" if the function succeeds, the return value is a handle to the new thread. "So we have to define a handle to store its return value. It also defines a DWORD Value dwthreadid pointing to the thread ID. Then we can use the createthread function to create our thread. The createthread function has six parameters:
Lpsecurity_attributes lpthreadattributes, // pointer to security attributes
DWORD dwstacksize, // initial thread stack size
Lpthread_start_routine lpstartaddress, // pointer to thread function
Lpvoid lpparameter, // argument for new thread
DWORD dwcreationflags, // creation flags
Lpdword lpthreadid // pointer to receive thread ID
The first parameter is set to null so that the handle cannot be inherited. The second parameter is set to 0 and the default stack size is used. The third parameter is the starting address of the thread function, that is, the function name of the thread function. The fourth parameter is null and no value is required to be passed to the thread function. The fifth parameter is 0. The thread will run immediately after the creation; the sixth parameter is set to the address pointing to the thread ID. After a thread is created, the thread function performs initialization and other operations. When the main function continues to be executed, the ID of the created thread can be output. We use the waitforsingleobject function in the main function to wait for the thread function to change to the signaled state. The two parameters are
Handle hhandle, // handle to object to wait
DWORD dwmilliseconds // time-out interval in milliseconds
The first parameter is the handle of the thread function, and the second parameter is set to infinite. At the end of the program, remember to use the closehandle function to close the thread so that the main function is finished.

In a thread function, we can simply do some work, such as setting a loop to output certain information. The source program is as follows:
# Include <windows. h>
# Include <iostream. h>
DWORD winapi threadfunc (handle thread)
{
Int I;
For (I = 0; I <10; I ++)
{
Cout <"A New thread has created! "<Endl;
}
Return 0;
}

Int main (INT argc, char * argv [])
{
Handle thread;
DWORD dwthreadid;
Thread =: createthread
(Null, 0, threadfunc, null, 0, & dwthreadid );
Cout <"The New thread ID is:" <dwthreadid <Endl;
: Waitforsingleobject (thread, infinite );
: Closehandle (thread );
Return 0;
}

-- Transfer from http://blog.csdn.net/richard_2010/archive/2006/03/05/616143.aspx

Related Article

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.