Compare commits
2 Commits
449978bda0
...
9dc1af921c
| Author | SHA1 | Date | |
|---|---|---|---|
| 9dc1af921c | |||
| e761625eab |
@@ -49,15 +49,11 @@ public:
|
|||||||
public:
|
public:
|
||||||
//! \~english Move constructor - transfers ownership of the lock.
|
//! \~english Move constructor - transfers ownership of the lock.
|
||||||
//! \~russian Конструктор перемещения - передает владение блокировкой.
|
//! \~russian Конструктор перемещения - передает владение блокировкой.
|
||||||
Pointer(Pointer && other): pv(other.pv) { other.can_unlock = false; };
|
Pointer(Pointer && other): pv(other.pv) { pv.mutex.lock(); };
|
||||||
|
|
||||||
//! \~english Destroys wrapper and releases the mutex.
|
//! \~english Destroys wrapper and releases the mutex.
|
||||||
//! \~russian Уничтожает обертку и освобождает мьютекс.
|
//! \~russian Уничтожает обертку и освобождает мьютекс.
|
||||||
~Pointer() {
|
~Pointer() { pv.mutex.unlock(); }
|
||||||
if (can_unlock) {
|
|
||||||
pv.mutex.unlock();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//! \~english Returns pointer access to the protected value.
|
//! \~english Returns pointer access to the protected value.
|
||||||
//! \~russian Возвращает указательный доступ к защищенному значению.
|
//! \~russian Возвращает указательный доступ к защищенному значению.
|
||||||
@@ -72,7 +68,6 @@ public:
|
|||||||
explicit Pointer(PIProtectedVariable<T> & v): pv(v) { pv.mutex.lock(); }
|
explicit Pointer(PIProtectedVariable<T> & v): pv(v) { pv.mutex.lock(); }
|
||||||
|
|
||||||
PIProtectedVariable<T> & pv;
|
PIProtectedVariable<T> & pv;
|
||||||
bool can_unlock = true;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//! \~english Replaces the protected value with \a v.
|
//! \~english Replaces the protected value with \a v.
|
||||||
|
|||||||
@@ -82,7 +82,9 @@ TEST(PIProtectedVariable_ThreadSafety, ConcurrentReadWrite) {
|
|||||||
for (int j = 0; j < NUM_ITERATIONS; ++j) {
|
for (int j = 0; j < NUM_ITERATIONS; ++j) {
|
||||||
auto val = pv.getRef();
|
auto val = pv.getRef();
|
||||||
(*val)++;
|
(*val)++;
|
||||||
|
auto val2 = pv.getRef();
|
||||||
writeCount++;
|
writeCount++;
|
||||||
|
ASSERT_EQ(writeCount, *val2);
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user