📄 jmutex.cpp
字号:
/* This file is a part of the JThread package, which contains some object- oriented thread wrappers for different thread implementations. Copyright (c) 2000-2004 Jori Liesenborgs (jori@lumumba.luc.ac.be) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.*/#include "jmutex.h"JMutex::JMutex(){ initialized = false;}JMutex::~JMutex(){ if (initialized) CloseHandle(mutex);}int JMutex::Init(){ if (initialized) return ERR_JMUTEX_ALREADYINIT; mutex = CreateMutex(NULL,FALSE,NULL);//The CreateMutex function creates a named or unnamed
//mutex object.
//mutex object :An interprocess synchronization object whose state is set to signaled when
//it is not owned by any thread, and nonsignaled when it is owned. Only one thread at a time can own a mutex.
//nonsignaled :The state of an object used for synchronization in one of the wait functions
//is either signaled or nonsignaled. A nonsignaled state can prevent the wait function from returning.
//signaled :The state of an object used for synchronization in one of the wait functions.
//A signaled state can enable the wait function to return.
//wait function :A function that blocks the execution of the calling thread until a
//specified set of conditions has been met.
if (mutex == NULL) return ERR_JMUTEX_CANTCREATEMUTEX; initialized = true; return 0;}int JMutex::Lock(){ if (!initialized) return ERR_JMUTEX_NOTINIT; WaitForSingleObject(mutex,INFINITE);//等待使mutex变为有信号
//The WaitForSingleObject function returns when one of the following occurs:
//The specified object is in the signaled state.
//The time-out interval elapses.
/*DWORD WaitForSingleObject(
HANDLE hHandle, // handle to object to wait for
DWORD dwMilliseconds // time-out interval in milliseconds);*/
/*dwMilliseconds :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. */
/*The WaitForSingleObject function checks the current state of the specified object. If the
object's state is nonsignaled, the calling thread enters an efficient wait state. The thread
consumes very little processor time while waiting for the object state to become signaled or
the time-out interval to elapse.*/ return 0;}int JMutex::Unlock(){ if (!initialized) return ERR_JMUTEX_NOTINIT; ReleaseMutex(mutex);//The ReleaseMutex function releases ownership of the specified mutex
//object. return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -