一些适用于Windows下的多線程同步的小工具類。
快速互斥鎖,封裝了臨界區的Windows API:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZuUmbv50LcNncvRXYjlGZul0ZulmbpxGd190LcNXZnFWbJ9CXt92Yuc2bsJGcwNmL3d3dvw1LcpDc0RHaiojIsJye.gif)
class FastMutex
{
private:
CRITICAL_SECTION m_Cs;
public:
FastMutex() { ::InitializeCriticalSection(&this->m_Cs); }
~FastMutex() { ::DeleteCriticalSection(&this->m_Cs); }
void Lock() { ::EnterCriticalSection(&this->m_Cs); }
bool TryLock() { return ::TryEnterCriticalSection(&this->m_Cs) ? true : false; }
void Unlock() { ::LeaveCriticalSection(&this->m_Cs); }
};
簡單封裝了Windows的信号量(Semaphore)的API。
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZuUmbv50LcNncvRXYjlGZul0ZulmbpxGd190LcNXZnFWbJ9CXt92Yuc2bsJGcwNmL3d3dvw1LcpDc0RHaiojIsJye.gif)
class FastSemaphore
HANDLE m_hSemaphore;
long m_lMaximumCount;
FastSemaphore(long lMaximumCount)
{
this->m_hSemaphore = ::CreateSemaphore(NULL, lMaximumCount, lMaximumCount, NULL);
if (this->m_hSemaphore == NULL) throw "Call to CreateSemaphore() failed. Could not create semaphore.";
this->m_lMaximumCount = lMaximumCount;
};
~FastSemaphore() { ::CloseHandle(this->m_hSemaphore); };
long GetMaximumCount() const { return this->m_lMaximumCount; };
void Inc() { ::WaitForSingleObject(this->m_hSemaphore, INFINITE); };
void Dec() { ::ReleaseSemaphore(this->m_hSemaphore, 1, NULL); };
void Dec(long lCount) { ::ReleaseSemaphore(this->m_hSemaphore, lCount, NULL); };
讀寫互斥鎖,多線程可以同時讀取同一個檔案,但是卻不能同時寫入同一個檔案,對某一個檔案的寫操作必須是某一個線程所獨占的。
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZuUmbv50LcNncvRXYjlGZul0ZulmbpxGd190LcNXZnFWbJ9CXt92Yuc2bsJGcwNmL3d3dvw1LcpDc0RHaiojIsJye.gif)
class ReadWriteMutex
FastMutex m_qMutex;
FastSemaphore m_qSemaphore;
ReadWriteMutex(long lMaximumReaders): m_qSemaphore(lMaximumReaders) {};
void lockRead() { m_qSemaphore.Inc(); };
void unlockRead() { m_qSemaphore.Dec(); };
void lockWrite()
m_qMutex.Lock();
for (int i = 0; i < maxReaders(); ++i) m_qSemaphore.Inc();
m_qMutex.Unlock();
void unlockWrite() { m_qSemaphore.Dec(m_qSemaphore.GetMaximumCount()); };
int maxReaders() const { return m_qSemaphore.GetMaximumCount(); };
區域鎖
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZuUmbv50LcNncvRXYjlGZul0ZulmbpxGd190LcNXZnFWbJ9CXt92Yuc2bsJGcwNmL3d3dvw1LcpDc0RHaiojIsJye.gif)
template <class M>
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZuUmbv50LcNncvRXYjlGZul0ZulmbpxGd190LcNXZnFWbJ9CXt92Yuc2bsJGcwNmL3d3dvw1LcpDc0RHaiojIsJye.gif)
class ScopedLock
inline ScopedLock(M& mutex): _mutex(mutex)
_mutex.Lock();
}
inline ~ScopedLock()
_mutex.Unlock();
M& _mutex;
ScopedLock();
ScopedLock(const ScopedLock&);
ScopedLock& operator = (const ScopedLock&);
曉得區域鎖咋用吧?
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZuUmbv50LcNncvRXYjlGZul0ZulmbpxGd190LcNXZnFWbJ9CXt92Yuc2bsJGcwNmL3d3dvw1LcpDc0RHaiojIsJye.gif)
void xxxFuc()
ScopeLock<FastMutex> mutex;
}
區域解鎖
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZuUmbv50LcNncvRXYjlGZul0ZulmbpxGd190LcNXZnFWbJ9CXt92Yuc2bsJGcwNmL3d3dvw1LcpDc0RHaiojIsJye.gif)
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZuUmbv50LcNncvRXYjlGZul0ZulmbpxGd190LcNXZnFWbJ9CXt92Yuc2bsJGcwNmL3d3dvw1LcpDc0RHaiojIsJye.gif)
class ScopedUnlock
inline ScopedUnlock(M& mutex, bool unlockNow = true): _mutex(mutex)
if (unlockNow)
_mutex.Unlock();
inline ~ScopedUnlock()
ScopedUnlock();
ScopedUnlock(const ScopedUnlock&);
ScopedUnlock& operator = (const ScopedUnlock&);
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZuUmbv50LcNncvRXYjlGZul0ZulmbpxGd190LcNXZnFWbJ9CXt92Yuc2bsJGcwNmL3d3dvw1LcpDc0RHaiojIsJye.gif)
與上面的區域鎖的操作相反。
NOTE:他們隻是簡單的小工具類,他們隻是保證了“能用”,當中可能有很多不足,或者不适用特别的情況。