C ++ multithreading (1)

Source: Internet
Author: User
C ++ multithreading (1)
Win multi-thread API

A simple example
It is relatively simple code to create 10 threads, of which 4th threads are suspended upon creation. When other threads run almost, 4th threads are resumed for execution. # Include <stdio. h>
# Include <stdlib. h>
# Include <windows. h>

# Define thread_num 10

DWORD winapi printthreads (lpvoid );

Int main ()
{
Handle hthread [thread_num];
DWORD dwthreadid [thread_num];

For (INT I = 0; I <thread_num; ++ I)
{
Int isstartimmediate = 0;
If (3 = I)
Isstartimmediate = create_suincluded;
Hthread [I] = createthread (null, // security attributes that shoshould be applied to the new thread,
// This is for NT. Use null to get the default security attributes. Use NULL for Win95
0,
// Default size of 1 MB can be passed by passing zero.
Printthreads,
// Function name: Address of the function where the new thread starts.
(Lpvoid) I,
// Parameter (void pointer): pointer to the 32 bit parameter that will be passed into the thread
Isstartimmediate,
// Flags to control the creation of the thread. Passing zero starts the thread immediately.

// Passing create_susponded susponds the thread until the resumethread () function is called.
& Dwthreadid [I]
// Pointer to a 32-bit variable that matches es the thread identifier.
);
If (hthread [I])
{
Printf ("thread launched successfully/N ");
}
}
Printf ("START sleep 100, and let other thread excute/N ");
Sleep (100 );

Printf ("START sleep 100, and thread 3 excute/N ");
Resumethread (hthread [3]);

Sleep (100 );

For (INT I = 0; I <thread_num; ++ I)
{
If (hthread [I])
{
Closehandle (hthread [I]); // you need to use this to release kernel objects when you are done using them.
// If a process exits without closing the thread handle,
// The operating system drops the reference counts for those objects.
// But if a process frequently creates threads without closing the handles,
// There cocould be hundreds of thread kernel objects lying around and these resource leaks can have a big hit on performance.
}
}
Return (0 );
}

// Function printthreads
DWORD winapi printthreads (lpvoid num)
{
For (INT I = 0; I <10; I ++)
Printf ("thread number is % d/N", num );
Return 0;
}

2. Descriptions of other basic APIs
If createthread () is called successfully, a handle and an ID are returned.
Closehandle () is used to close an opened object handle. This object handle can be a thread handle or a handle of other kernel objects such as processes and semaphores.

Suspendthread (handle) allows developers to suspend the thread specified by handle. If the thread to be suspended occupies shared resources, it may lead to a deadlock.
Resumethread (handle) restores the specified thread.

Terminatethread () immediately terminates the thread without any cleanup.
The exitthread () thread function calls back when it returns, so we generally do not display the call.

Exitthread is the recommended method to end a thread. When this function is called, the stack of the current thread is released and the thread is terminated. Compared with the terminatethread function, this can better clear the DLL attached to this thread. however, exitthread () will cause the thread to terminate before the cleaning constructor/automatic variable, so we 'd better not display the call to exitthread ().

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.