Multithreading-Mutually exclusive variables

Source: Internet
Author: User

The first of the CreateMutex

function function: Create mutex (note vs. event creation function)

Function Prototypes:

HANDLE CreateMutex (

Lpsecurity_attributeslpmutexattributes,

Boolbinitialowner,

Lpctstrlpname

);

Function Description:

The first parameter represents the security control, which is typically passed directly to null.

The second parameter is used to determine the initial owner of a mutex. If passed in true means that the thread ID number of the thread that created it is logged inside the mutex object and the recursive count is set to 1, because the thread ID is nonzero, the mutex is not in the triggered state. If False is passed in, the thread ID number inside the mutex object is set to NULL, and the recursive count is set to 0, which means that the mutex is not occupied by any thread and is in a triggered state.

The third parameter is used to set the name of the mutex, in which threads in multiple processes make sure that they are accessing the same mutex by name.

function Access Value:

Successfully returns a handle representing the mutex, and the failure returns NULL.

Second Open mutex

Function Prototypes:

HANDLE OpenMutex (

Dworddwdesiredaccess,

Boolbinherithandle,

Lpctstrlpname//Name

);

Function Description:

The first parameter represents access rights, and the mutex is generally passed into mutex_all_access. Detailed explanations can be viewed in the MSDN documentation.

The second parameter represents the mutex handle inheritance, which is generally passed to true.

The third parameter represents a name. After a thread in a process creates a mutex, the thread in the other process can find the mutex through this function.

function Access Value:

Successfully returns a handle representing the mutex, and the failure returns NULL.

Third Trigger Mutex

Function Prototypes:

BOOL ReleaseMutex (Handlehmutex)

Function Description:

You should call the wait function before accessing the mutex, call ReleaseMutex () at the end of the visit to indicate that you have ended the access, and the other threads can begin to access it.

Last cleanup mutex

Because the mutex is a kernel object, it is possible to use CloseHandle () (which is the same for all kernel objects).

#include <stdio.h> #include <process.h> #include <windows.h>handle hmutex;int Tickets=100;dword WINAPI Fun1 (VOID *lp) {while (true) {        WaitForSingleObject (hmutex,infinite), if (tickets>0) {printf ("Thread1%d\n ", tickets--); ReleaseMutex (Hmutex);} Else{releasemutex (Hmutex); break;}} return 0;} DWORD WINAPI Fun2 (VOID *lp) {while (true) {       WaitForSingleObject (hmutex,infinite);   if (tickets>0)   {   printf ("Thread2%d\n", tickets--);   ReleaseMutex (Hmutex);   }   else   {   ReleaseMutex (Hmutex);   break;   }} return 0;} int main () {Hmutex=createmutex (null,false,null); HANDLE Handle1=createthread (null,0,fun1,null,0,null); HANDLE Handle2=createthread (null,0,fun2,null,0,null); CloseHandle (HANDLE1); CloseHandle (handle2);    Sleep (3000); CloseHandle (Hmutex); return 0;}

Reference: http://blog.csdn.net/morewindows/article/details/7470936

Multithreading-Mutually exclusive variables

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.