互斥体
同步的简单形式
互斥体(mutual exclusion)是实现“互相排斥”(mutual exclusion)同步的简单形式。互斥体禁止多个线程同时进入受保护的代码“临界区”(critical section)。
简介
在任意时刻,只有一个线程被允许进入代码保护区。任何线程在进入临界区之前,必须获取(acquire)与此区域相关联的互斥体的所有权。如果已有另一线程拥有了临界区的互斥体,其他线程就不能再进入其中。这些线程必须等待,直到当前的属主线程释放(release)该互斥体。什么时候需要使用互斥体呢?互斥体用于保护共享的易变代码,也就是,全局或静态数据。这样的数据必须通过互斥体进行保护,以防止它们在多个线程同时访问时损坏。
创建
在VC中,我们用CreateMutex函数创建互斥体。
HANDLE WINAPI CreateMutex(
__in_opt LPSECURITY_ATTRIBUTES lpMutexAttributes,
__in BOOL bInitialOwner,
__in_opt LPCTSTR lpName
);
应用
#include <windows.h>
#include <stdio.h>
#define THREADCOUNT 2
HANDLE ghMutex;
DWORD WINAPI WriteToDatabase( LPVOID );
void main()
{
HANDLE aThread[THREADCOUNT];
DWORD ThreadID;
int i;
// Create a mutex with no initial owner
ghMutex = CreateMutex(
NULL, // default security attributes
FALSE, // initially not owned
NULL); // unnamed mutex
if (ghMutex == NULL)
{
return;
}
// Create worker threads
for( i=0; i < THREADCOUNT; i++ )
{
aThread[i] = CreateThread(
NULL, // default security attributes
0, // default stack size
(LPTHREAD_START_ROUTINE) WriteToDatabase,
NULL, // no thread function arguments
0, // default creation flags
&ThreadID); // receive thread identifier
if( aThread[i] == NULL )
{
return;
}
}
// Wait for all threads to terminate
WaitForMultipleObjects(THREADCOUNT, aThread, TRUE, INFINITE);
// Close thread and mutex handles
for( i=0; i < THREADCOUNT; i++ )
CloseHandle(aThread[i]);
CloseHandle(ghMutex);
}
DWORD WINAPI WriteToDatabase( LPVOID lpParam )
{
DWORD dwCount=0, dwWaitResult;
// Request ownership of mutex.
while( dwCount < 20 )
{
dwWaitResult = WaitForSingleObject(
ghMutex, // handle to mutex
INFINITE); // no time-out interval
switch (dwWaitResult)
{
// The thread got ownership of the mutex
case WAIT_OBJECT_0:
__try {
// TODO: Write to the database
dwCount++;
}
__finally {
// Release ownership of the mutex object
if (! ReleaseMutex(ghMutex))
{
// Handle error.
}
}
break;
// The thread got ownership of an abandoned mutex
// The database is in an indeterminate state
case WAIT_ABANDONED:
return FALSE;
}
}
return TRUE;
}
备注信息
微软MSDN中关于互斥体的备注
The handle returned byCreateMutexhas MUTEX_ALL_ACCESS access to the new mutex object and can be used in any function that requires a handle to a mutex object.
通过CreateMutex函数返回的句柄拥有MUTEX_ALL_ACCESS 权限,并且可以在任何函数中请求这个互斥体对象句柄。
Any thread of the calling process can specify the mutex-object handle in a call to one of thewait functions.
任何调用进程的线程均可以在WAIT系列的函数中指定这个互斥体对象句柄。
The single-object wait functions return when the state of the specified object is signaled.
单个对象的等待函数(例如:SignalObjectAndWait,WaitForSingleObject, andWaitForSingleObjectEx)会在指定对象的状态变为受信状态后返回。
The multiple-object wait functions can be instructed to return either when any one or when all of the specified objects are signaled. When a wait function returns, the waiting thread is released to continue its execution.
多对象的等待函数将等待任意一个或全部对象都变成受信状态后返回(取决于函数的参数设置)。当等待函数返回,等待线程将继续执行下去。
The state of a mutex object is signaled when it is not owned by any thread.
参考资料
最新修订时间:2024-11-07 01:08
目录
概述
简介
创建
参考资料