互斥对象实现线程同步

首先按照正常的举出一Demo,如下:


#include<windows.h>
#include<iostream.h>
DWORD WINAPI FuncThreadone(LPVOID lpParameter);
DWORD WINAPI FuncThreadTwo(LPVOID lpParameter);

int index=0;
int tickets=100;

void main(){
 HANDLE hThread1,hThread2;
 hThread1=CreateThread(NULL,0,FuncThreadone,NULL,0,NULL);
 hThread2=CreateThread(NULL,0,FuncThreadTwo,NULL,0,NULL);
 CloseHandle(hThread1);
 CloseHandle(hThread2);
 cout<<"main thread is running"<<endl;
 Sleep(10);
 Sleep(4000);
}

DWORD WINAPI FuncThreadone(LPVOID lpParameter){

 cout<<"thread one is running !"<<endl;
 while(1){
  if(tickets>0){
   cout<<"thread1 sell ticket : "<<tickets--<<endl;
  }else{
   break;
  }
 }
 return 0;

}

DWORD WINAPI FuncThreadTwo(LPVOID lpParameter){

 cout<<"thread two is running !"<<endl;
 while(1){
  if(tickets>0){
   cout<<"thread2 sell ticket : "<<tickets--<<endl;
  }else{
   break;
  }
 }
 return 0;

}

 

我们在上面Demo中添加互斥事件,互斥需要几个函数:

This function creates a named or unnamed mutex object.

HANDLE CreateMutex( 
  LPSECURITY_ATTRIBUTES lpMutexAttributes, 
  BOOL bInitialOwner, 
  LPCTSTR lpName 
);

Parameters

lpMutexAttributes
[in] Ignored. Must be NULL.
如果忽略,必须设置为NULL
bInitialOwner
[in] Boolean that 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.
Boolean类型,指定互斥对象初始化的拥有者,如果设置为真,则创建这个互斥对象的线程获得该对象的所有权;否则,该线程将不获的所创建互斥对象的所有权.
lpName
[in] Long pointer to a null-terminated string specifying the name of the mutex object. The name is limited to MAX_PATH characters and can contain any character except the backslash path-separator character (\). Name comparison is case sensitive.

If lpName matches the name of an existing named mutex object, the bInitialOwner parameter is ignored because it has already been set by the creation process.

If lpName is NULL, the mutex object is created without a name.

If lpName matches the name of an existing event, semaphore, 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.

如果设置为NULL,则创建一个匿名互斥对象.

This function returns when the specified object is in the signaled state or when the time-out interval elapses.

DWORD WaitForSingleObject( 
  HANDLE hHandle, 
  DWORD dwMilliseconds 
); 

Parameters

hHandle
[in] Handle to the object. For a list of the object types whose handles can be specified, see the Remarks section.
请求对象的句柄,一旦互斥对象处于有信号状态,则函数返回,如果互斥对象一直处于无信号状态,则该函数会一直等待下去,即当前线程会暂停.
dwMilliseconds
[in] Specifies the time-out interval, in milliseconds. The function returns if the interval elapses, even if the object's state is nonsignaled. If dwMilliseconds is zero, the function tests the object's state and returns immediately. If dwMilliseconds is INFINITE, the function's time-out interval never elapses.
指定等待的时间间隔,以ms为单位,如果超过这个指定的时间间隔,即使在无信号状态下,也会返回,一旦返回,线程将继续.
 
需要该函数得到返回,根据上面的分析就可以有两种方法让其返回:
<1> : 指定的对象变成有信号状态;
<2> : 指定的等待时间间隔到了,即timeout.

This function releases ownership of the specified mutex object.

BOOL ReleaseMutex( 
  HANDLE hMutex 
);

Parameters

hMutex
[in] Handle to the mutex object. The CreateMutex function returns this handle.

Return Values

Nonzero indicates success. Zero indicates failure. To get extended error information, call GetLastError.

当共享资源访问结束后,应该释放对象的所有权,也就是该对象处于已通知状态.

 

现在更新上面demo:


#include<windows.h>
#include<iostream.h>
DWORD WINAPI FuncThreadone(LPVOID lpParameter);
DWORD WINAPI FuncThreadTwo(LPVOID lpParameter);

int index=0;
int tickets=100;
HANDLE hMutex;

void main(){
 HANDLE hThread1;
 HANDLE hThread2;
 hMutex=CreateMutex(NULL,0,NULL);

 hThread1=CreateThread(NULL,0,FuncThreadone,NULL,0,NULL);
 hThread2=CreateThread(NULL,0,FuncThreadTwo,NULL,0,NULL);
 CloseHandle(hThread1);
 CloseHandle(hThread2);
 cout<<"main thread is running"<<endl;
 Sleep(10);
 Sleep(4000);
}

DWORD WINAPI FuncThreadone(LPVOID lpParameter){

 cout<<"thread one is running !"<<endl;
 while(1){
  WaitForSingleObject(hMutex,INFINITE);
  if(tickets>0){
   cout<<"thread1 sell ticket : "<<tickets--<<endl;
  }else{
   break;
  }
  ReleaseMutex(hMutex);
 }
 return 0;

}

DWORD WINAPI FuncThreadTwo(LPVOID lpParameter){

 cout<<"thread two is running !"<<endl;
 while(1){
  WaitForSingleObject(hMutex,INFINITE);
  if(tickets>0){
   cout<<"thread2 sell ticket : "<<tickets--<<endl;
  }else{
   break;
  }
  ReleaseMutex(hMutex);
 }
 return 0;

}

运行结果:

互斥对象实现线程同步

转载于:https://www.cnblogs.com/MMLoveMeMM/articles/3054826.html