Multithreading for thread synchronization--inter-secondary objects

Source: Internet
Author: User

The inter-secondary object implements thread synchronization.

1. Manipulate the reciprocal objects using the API functions.

The function CreateMutex is used to create and return reciprocal objects.

Function Prototypes:

HANDLE CreateMutex (  lpsecurity_attributes lpmutexattributes,  //SD  BOOL Binitialowner,                       //Initial Owner  LPCTSTR lpname                            //object name);

The Lpmutexattributes parameter specifies the security properties of the newly created reciprocal object. If this parameter is NULL, it indicates that the reciprocal object has a default security attribute.

The parameter Binitialowner represents the owner of the reciprocal object. If true, indicates that the thread that created the interop object owns its ownership. If False, the thread that represents the created interop object cannot have ownership of the reciprocal object.

The parameter lpname represents the name of the cross-secondary object. If this parameter is NULL, the program creates an anonymous object, and if you specify a value for the parameter, you can open a named reciprocal object by calling the function OpenMutex.

Calling the function ReleaseMutex frees the object's ownership, which means that the reciprocal object is in a signaled state.

Function Prototypes:

BOOL ReleaseMutex (  HANDLE hmutex   //HANDLE to mutex);

The following is the code implemented by the C language:

#include <stdio.h> #include <windows.h>dword WINAPI myfun1 (LPVOID lpparameter); Declares the thread function DWORD WINAPI myfun2 (LPVOID lpparameter); HANDLE Hmutex;int A = 0;int main () {Hmutex =:: CreateMutex (NULL, FALSE, NULL);     HANDLE h1, h2;h1 =:: CreateThread (NULL, 0, myfun1, NULL, 0, NULL); Create thread printf ("Thread 1 starts running!\r\n"); H2 =:: CreateThread (NULL, 0, myfun2, NULL, 0, NULL);p rintf ("Thread 2 starts running!\r\n");:: CloseHandle (H1);:: CloseHandle (H2);:: Sleep (10000); return 0;}             DWORD WINAPI myfun1 (LPVOID lpparameter) {while (1) {:: WaitForSingleObject (Hmutex, INFINITE);                          Request reciprocal Objects if (a <) {A + 1;::sleep (1) is;p rintf ("Thread Count%d\r\n", a);:: ReleaseMutex (Hmutex); Release the reciprocal object handle}else{::releasemutex (Hmutex); return 0;} DWORD WINAPI myfun2 (LPVOID lpparameter) {while (1) {:: WaitForSingleObject (Hmutex, INFINITE); if (a < +) {A + = 1;:: Sleep (rintf);p ("Thread 2 is counting%d\r\n", a);:: ReleaseMutex (Hmutex);} Else{::releasemutex (Hmutex); break;}} return 0;}



2. Using the CMutex class


The creation of a CMutex class object is implemented through its constructors.

Constructor prototypes:

CMutex (BOOL binitiallyown = FALSE, LPCTSTR lpszName = null, lpsecurity_attributes lpsaattribute = null);


Calling function lock and unlock locks and unlocks the area protected by the object.


Code:

#include <stdio.h> #include <afxmt.h>dword WINAPI myfun1 (LPVOID lpparameter);                    Declares the thread function DWORD WINAPI myfun2 (LPVOID lpparameter); CMutex Hmutex (NULL, FALSE, NULL), int a = 0;int main () {HANDLE h1, h2;h1 =:: CreateThread (NULL, 0, myfun1, NULL, 0, NULL); 
   //Create thread printf ("Thread 1 starts running!\r\n"), H2 =:: CreateThread (NULL, 0, myfun2, NULL, 0, NULL);p rintf ("Thread 2 starts running!\r\n");:: CloseHandle (H1);:: CloseHandle (H2);:: Sleep (10000); return 0;} DWORD WINAPI myfun1 (LPVOID lpparameter) {while (1) {Hmutex. Lock (INFINITE);                            Lock the reciprocal objects if (a <) {A + = 1;::sleep (+);p rintf ("Thread 1:%d\r\n", a); Hmutex. Unlock ();                              Unlocks the}else{hmutex object. Unlock (); break;}} return 0; }dword WINAPI myfun2 (LPVOID lpparameter) {while (1) {Hmutex. Lock (INFINITE), if (a <) {A + = 1;::sleep (;p) rintf ("Thread 2:%d\r\n", a); Hmutex. Unlock ();} Else{hmutex. Unlock (); break;}} return 0; }


Results:





Multithreading for thread synchronization--inter-secondary objects

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.