Multi-threaded-6

Source: Internet
Author: User
Tags mutex

The ID of the thread and the mutex object counter

void Main () {
HANDLE Handle to hthread1;//thread
HANDLE hThread2;
Hthread1=createthread (Null,0,fun1proc,null,0,null);
Hthread2=createthread (Null,0,fun2proc,null,0,null);
CloseHandle (HTHREAD1);//closes the handle and does not terminate the thread, indicating that the handle is not interested in the main thread.
When the handle is closed, the usage count of the thread kernel object of the new thread is decremented,
The usage count is also decremented when the thread finishes executing
When use count is 0 o'clock, the system releases the thread kernel object
CloseHandle (HTHREAD2);
Hmutex=createmutex (null,false,null);//Create an anonymous mutex object. False no thread owns the mutex object.

The operating system sets the mutex to a notified state, which is signaled

WaitForSingleObject (hmutex,infinite);//The same Mian main thread, Hmutex plus 1, became 2

ReleaseMutex (Hmutex);

ReleaseMutex (Hmutex);//Must be released two times to make the Hmutex ID 0, in order to enable two threads in multithreading-5 to request to the mutex object Sleep (4000);//The main thread abandons the execution power and enters the waiting state.
}

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.