標籤:多線程 互斥
多線程同步之互斥對象
作者:vpoetmail:[email protected]
在http://blog.csdn.net/u013018721/article/details/46637215一文中介紹了使用臨界區對賣票問題進行線程間同步,本文將在上文的基礎上,使用互斥對象對線程進行同步。
首先看看windows API
該函數建立一個命名或者不命名的互斥對象
lpMutexAttributes
[in] Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. IflpMutexAttributes is NULL, the handle cannot be inherited.
參數lpMutexAttributes為互斥對象的安全屬性,如果為NULL,則使用預設的安全屬性
bInitialOwner
[in] Specifies the initial owner of the mutex object. If this value is TRUE and the caller created the mutex, the calling thread obtains ownership of the mutex object. Otherwise, the calling thread does not obtain ownership of the mutex. To determine if the caller created the mutex, see the Return Values section.
參數bInitialOwner指定該互斥對象的擁有者,若為TRUE則表明建立互斥對象的線程對其有所有權,而反之則表示建立互斥對象的線程不能對其擁有所有權
lpName
[in] Pointer to a null-terminated string specifying the name of the mutex object. The name is limited to MAX_PATH characters. Name comparison is case sensitive.
If lpName matches the name of an existing named mutex object, this function requests MUTEX_ALL_ACCESS access to the existing object. In this case, thebInitialOwner parameter is ignored because it has already been set by the creating process. If the lpMutexAttributes parameter is not NULL, it determines whether the handle can be inherited, but its security-descriptor member is ignored.
If lpName is NULL, the mutex object is created without a name.
If lpName matches the name of an existing event, semaphore, waitable timer, job, or file-mapping object, the function fails and the GetLastError function returns ERROR_INVALID_HANDLE. This occurs because these objects share the same name space.
Terminal Services: The name can have a "Global\" or "Local\" prefix to explicitly create the object in the global or session name space. The remainder of the name can contain any character except the backslash character (\). For more information, see Kernel Object Name Spaces.
參數lpName指定是一個命名的互斥對象還是一個匿名的互斥對象,它們之間的區別我們留待後面再學習
該函數釋放互斥對象的所有權,也就是說該函數將互斥對象置為有訊號的狀態
該函數的參數即為CreateMutex函數返回的控制代碼對象
在利用互斥事件進行線程同步的時候我們還需要使用一個函數WaitForSingleObject該函數將等待互斥對象有狀態才返回否則一直阻塞。
接下來我們來實際動手看看,注意在CreateMutex函數的第二個參數我們置為FALSE,因為我們需要在主線程中建立該互斥對象,但是把這個互斥對象的所有權需要輪換的交給兩個子線程交替的賣票。
代碼如下:
#include <windows.h>#include <stdio.h>static int number=10;HANDLE Mutex;DWORD WINAPI ThreadOne(LPVOID lpParameter){while(1){WaitForSingleObject(Mutex,INFINITE); if(number>0){printf("視窗1售出第%d張票...\n",number);number--;Sleep(1000);}ReleaseMutex(Mutex);}return 0;}DWORD WINAPI ThreadTwo(LPVOID lpParameter){while(1){WaitForSingleObject(Mutex,INFINITE);if(number>0){printf("視窗2售出第%d張票...\n",number);Sleep(1000);number--;}ReleaseMutex(Mutex);}return 0;}int main(){HANDLE HOne,HTwo;Mutex=CreateMutex(NULL,FALSE,NULL);printf("***********************vpoet******************\n");HOne=CreateThread(NULL,0,ThreadOne,NULL,0,NULL);printf("視窗1售票開始:\n");HTwo=CreateThread(NULL,0,ThreadTwo,NULL,0,NULL);printf("視窗2售票開始:\n");CloseHandle(HOne);CloseHandle(HTwo);while(TRUE){if(number==0){printf("不好意思,票賣完了!\n");CloseHandle(Mutex);return 0;}else{continue;}}return 0;}
運行結果:
嗯 沒問題,達到我們要求了。
Ok,下次博文我們再一起學習使用事件對象進行線程間同步。
多線程同步之互斥對象