Add concurrent test for swcv
This commit is contained in:
1 parent
e815dade3a
commit
11252b4300
2 files changed
+38
-8
No files matched your search
@@ -55,14 +55,42 @@ TEST_CASE("SingleWriterConcurrentVector", "[collection]") {
|
||||
REQUIRE(chunkvec.size() == 9999);
|
||||
}
|
||||
SECTION("ONCE MORE, WITH THREADS") {
|
||||
// SingleWriterConcurrentVector<size_t> intvec;
|
||||
// SingleWriterConcurrentVector<Chunk> chunkvec;
|
||||
// std::thread writer([&]() {
|
||||
// });
|
||||
// auto reader = [&]() {
|
||||
// };
|
||||
// std::vector<std::thread> readers;
|
||||
// writer.join();
|
||||
const size_t OUTTER = 100;
|
||||
const size_t INNER = 1000;
|
||||
SingleWriterConcurrentVector<Chunk> chunkvec;
|
||||
std::thread writer([&]() {
|
||||
// write a million elements to the vec
|
||||
for (size_t a = 0; a < OUTTER; ++a) {
|
||||
for (size_t b = 0; b < INNER; ++b) {
|
||||
auto x = 1000 * a + b;
|
||||
chunkvec.push_back({x,x,x,x});
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
}
|
||||
});
|
||||
std::vector<std::thread> readers;
|
||||
for (size_t x = 0; x < 10; ++x) {
|
||||
readers.push_back(std::thread([&]() {
|
||||
// read until we have read a million elements
|
||||
size_t goal = OUTTER * INNER;
|
||||
size_t count = 0;
|
||||
while(count != goal) {
|
||||
count = 0;
|
||||
for (const auto& item : chunkvec) {
|
||||
// If theses tests are wrong it may be that item is partially constructed
|
||||
REQUIRE(item.a == item.d);
|
||||
REQUIRE(item.c == item.d);
|
||||
REQUIRE(item.b == item.c);
|
||||
REQUIRE(item.a == item.b);
|
||||
++count;
|
||||
}
|
||||
}
|
||||
}));
|
||||
}
|
||||
writer.join();
|
||||
for (auto& thread : readers) {
|
||||
thread.join();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user