sem.cpp

来自「使用ASPI包装成的一些通用类」· C++ 代码 · 共 50 行

CPP
50
字号
////////////////////////////////////////////////////////////
//
// Module SEM.CPP
//
// ASPI class library
// Semaphore support
//
// Project: A Programmer's Guide to SCSI
//
// Copyright (C) 1997, Brian Sawert
// Portions copyright (C) 1995, Larry Martin
// All rights reserved
//
////////////////////////////////////////////////////////////


#include "sem.hpp"


MutexSemaphore::MutexSemaphore()
   {
   Handle = CreateMutex(NULL,FALSE,NULL);
   // Not much to do on failure
   }

MutexSemaphore::~MutexSemaphore()
   {
   if (Handle)
      CloseHandle(Handle);
   }

int MutexSemaphore::RequestMutex(long timeout)
   {
   int x = WaitForSingleObject(Handle,(timeout == -1) ? INFINITE : timeout);
   switch (x)
      {
      case WAIT_OBJECT_0:
      case WAIT_ABANDONED:
         return 0;
      default:
         return -1;
      }
   }


int MutexSemaphore::ReleaseMutex()
   {
   return (::ReleaseMutex(Handle) == TRUE) ? 0 : -1;
   }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?