The first thread I created and the first thread I created

Source: Internet
Author: User

The first thread I created and the first thread I created
# Include <windows. h> // contains the corresponding header file
# Include <stdio. h>

Dword winapi myfun1 (LPVOID lpParameter); // declare a thread function


Int main () // main Function
{
HANDLE h1, h2; // defines the HANDLE variable
H1 = CreateThread (NULL, 0, myfun1, NULL, 0, NULL); // create thread 1
CloseHandle (h1); // closes the thread handle object
While (1)
{
If (getchar () = 'q') // if the user inputs the character q
{
Return 0; // The program Exits normally.
}
Else // if the character entered by the user is not q
{
: Sleep (100); // program Sleep
}
}
Return 0;
}


Dword winapi myfun1 (LPVOID lpParameter) // implement thread functions respectively
{
Printf ("thread 1 is running! \ R \ n "); // output information
Return 0; // normal end thread function
}



How to Create a thread in C ++

Check whether your thread is successfully created. In addition, even with multiple threads, the CPU is actually executed separately in scheduling mode, rather than running in parallel. You can try the output in the thread.

CreateThread (NULL, 0, ThreadProc, NULL );

The first parameter is a pointer to the security attribute, and NULL is enough.
The second parameter is the initial thread stack size, which can be 0. WINDOWS will automatically allocate
The third parameter points to the thread function, which must be in this way dword winapi ThreadProc (LPVOID pParam)
The fourth parameter is the parameter passed to the thread function. If yes, it is passed. If no, It is NULL.
The fifth parameter is the thread startup mode. It can be create_suincluded suspended or NULL started immediately.
The last parameter is the ID of the created thread.

If the call succeeds, the thread handle is returned.

Create a single-document-type MFC Application to create two threads for the program. The first thread can: Take the two variables x and y to a certain level

It can be processed based on the object name "thread" and does not need to be determined by "pdate ".

For the thread object, determine whether the thread exists or not:

Bool B = thread. IsAlive; true is returned, otherwise it does not exist

End this thread:
If (thread. IsAlive ){
Thread. Abort ();
}

Adopt it.

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.