diff options
author | Tor Egge <Tor.Egge@broadpark.no> | 2020-12-03 19:46:21 +0100 |
---|---|---|
committer | Tor Egge <Tor.Egge@broadpark.no> | 2020-12-03 19:46:35 +0100 |
commit | 13148035ce5843881e89f14ad2d644ac58a1b7ef (patch) | |
tree | d16f988e92abbe6580e77de8220e3f7d41e893c9 /vespalib/src/tests/array | |
parent | 54e2f0d40cad9ee1d442963e4dc765796fbac96b (diff) |
Use std::atomic.
Diffstat (limited to 'vespalib/src/tests/array')
-rw-r--r-- | vespalib/src/tests/array/array_test.cpp | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/vespalib/src/tests/array/array_test.cpp b/vespalib/src/tests/array/array_test.cpp index ee8263183ca..a5dfd1472a6 100644 --- a/vespalib/src/tests/array/array_test.cpp +++ b/vespalib/src/tests/array/array_test.cpp @@ -4,6 +4,7 @@ #include <vespa/vespalib/stllike/string.h> #include <vespa/vespalib/testkit/testapp.h> #include <deque> +#include <atomic> using namespace vespalib; @@ -27,16 +28,16 @@ std::ostream & operator << (std::ostream & os, const Array<T> & a) class Clever { public: - Clever() : _counter(&_global) { *_counter = *_counter + 1; } - Clever(volatile size_t * counter) : + Clever() : _counter(&_global) { (*_counter)++; } + Clever(std::atomic<size_t> * counter) : _counter(counter) { - *_counter = *_counter + 1; + (*_counter)++; } Clever(const Clever & rhs) : _counter(rhs._counter) { - *_counter = *_counter + 1; + (*_counter)++; } Clever & operator = (const Clever & rhs) { @@ -50,12 +51,12 @@ public: { std::swap(_counter, rhs._counter); } - ~Clever() { *_counter = *_counter - 1; } + ~Clever() { (*_counter)--; } static size_t getGlobal() { return _global; } bool operator == (const Clever & b) const { return _counter == b._counter; } private: - volatile size_t * _counter; - static size_t _global; + std::atomic<size_t> * _counter; + static std::atomic<size_t> _global; }; std::ostream & operator << (std::ostream & os, const Clever & clever) @@ -114,7 +115,7 @@ TEST("test basic array functionality") testArray(a, b); EXPECT_TRUE(a == a); EXPECT_FALSE(a == b); - size_t counter(0); + std::atomic<size_t> counter(0); testArray(Clever(&counter), Clever(&counter)); EXPECT_EQUAL(0ul, counter); } @@ -155,11 +156,11 @@ TEST("test that organic growth is by 2 in N and reserve resize are exact") EXPECT_EQUAL(2048u, c.capacity()); } -size_t Clever::_global = 0; +std::atomic<size_t> Clever::_global = 0; TEST("test complicated") { - volatile size_t counter(0); + std::atomic<size_t> counter(0); { EXPECT_EQUAL(0ul, Clever::getGlobal()); Clever c(&counter); |