Tidied SyncSafe template class
This commit is contained in:
@@ -10,8 +10,6 @@ namespace Bloom
|
||||
* Just a convenient template that allows us to create thread safe types without having to write
|
||||
* the bloat of mutexes, unique_locks, etc etc.
|
||||
*
|
||||
* @TODO Might be an idea to use an off-the-shelf solution for this, as there are a few available.
|
||||
*
|
||||
* @tparam Type
|
||||
*/
|
||||
template<typename Type>
|
||||
@@ -20,36 +18,25 @@ namespace Bloom
|
||||
public:
|
||||
SyncSafe() = default;
|
||||
|
||||
explicit SyncSafe(Type value): value(value) {};
|
||||
explicit SyncSafe(Type value)
|
||||
: value(value)
|
||||
{}
|
||||
|
||||
void setValue(Type value) {
|
||||
void setValue(const Type& value) {
|
||||
auto lock = std::unique_lock(this->mutex);
|
||||
this->value = value;
|
||||
};
|
||||
}
|
||||
|
||||
Type getValue() {
|
||||
auto lock = std::unique_lock(this->mutex);
|
||||
Type& getValue() {
|
||||
return this->value;
|
||||
};
|
||||
|
||||
Type& getReference() {
|
||||
return this->value;
|
||||
};
|
||||
|
||||
void lock() {
|
||||
this->mutex.lock();
|
||||
};
|
||||
|
||||
void unlock() {
|
||||
this->mutex.unlock();
|
||||
};
|
||||
}
|
||||
|
||||
std::unique_lock<std::mutex> acquireLock() {
|
||||
return std::unique_lock(this->mutex);
|
||||
};
|
||||
}
|
||||
|
||||
private:
|
||||
std::mutex mutex;
|
||||
Type value;
|
||||
std::mutex mutex;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -29,8 +29,9 @@ namespace Bloom
|
||||
Thread& operator = (Thread&& other) = delete;
|
||||
|
||||
virtual ThreadState getThreadState() {
|
||||
auto lock = this->state.acquireLock();
|
||||
return this->state.getValue();
|
||||
};
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void setThreadState(ThreadState state) {
|
||||
|
||||
Reference in New Issue
Block a user