thread/Mutex: use std::unique_lock to implement ScopeLock
This commit is contained in:
parent
a421c1dbfb
commit
9c11184238
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2009-2015 Max Kellermann <max@duempel.org>
|
* Copyright (C) 2009-2016 Max Kellermann <max@duempel.org>
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* modification, are permitted provided that the following conditions
|
* modification, are permitted provided that the following conditions
|
||||||
@ -30,6 +30,8 @@
|
|||||||
#ifndef THREAD_MUTEX_HXX
|
#ifndef THREAD_MUTEX_HXX
|
||||||
#define THREAD_MUTEX_HXX
|
#define THREAD_MUTEX_HXX
|
||||||
|
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
|
|
||||||
#include "CriticalSection.hxx"
|
#include "CriticalSection.hxx"
|
||||||
@ -43,26 +45,13 @@ class Mutex : public PosixMutex {};
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
class ScopeLock {
|
class ScopeLock {
|
||||||
Mutex &mutex;
|
std::unique_lock<Mutex> lock;
|
||||||
|
|
||||||
bool active = true;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ScopeLock(Mutex &_mutex):mutex(_mutex) {
|
ScopeLock(Mutex &_mutex):lock(_mutex) {}
|
||||||
mutex.lock();
|
|
||||||
};
|
|
||||||
|
|
||||||
~ScopeLock() {
|
|
||||||
if (active)
|
|
||||||
mutex.unlock();
|
|
||||||
};
|
|
||||||
|
|
||||||
ScopeLock(const ScopeLock &other) = delete;
|
|
||||||
ScopeLock &operator=(const ScopeLock &other) = delete;
|
|
||||||
|
|
||||||
void Unlock() {
|
void Unlock() {
|
||||||
mutex.unlock();
|
lock.unlock();
|
||||||
active = false;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user