DEV: atomic strength for queues

This commit is contained in:
2021-09-30 19:19:44 +03:00
parent d2f8165ec6
commit d9da3917a9
6 changed files with 139 additions and 41 deletions
+51
View File
@@ -34,6 +34,14 @@
#include <array>
#include <type_traits>
#include <cstring>
#ifndef WIN_TRHEADS
#include <mutex>
#include <thread>
#else
#include <mingw.thread.h>
#include <mingw.mutex.h>
#endif
namespace Tdeque {
using namespace tbx;
@@ -415,4 +423,47 @@ namespace Tdeque {
EXPECT_EQ(9, check_it); // run through all
}
TEST(Tdeque, race) {
constexpr size_t N = 1000000;
deque<int, N, true> q;
int result[N];
auto push_front = [&](){
for (size_t i=1 ; i<=N ; ++i) q.push_front(i);
};
auto push_back = [&](){
for (size_t i=1 ; i<=N ; ++i) q.push_back(i);
};
auto pop_front = [&](){
for (size_t i=0 ; i<N ; ) {
result[i] = q.pop_front();
if (result[i] != int{})
++i;
}
};
auto pop_back = [&](){
for (size_t i=0 ; i<N ; ) {
result[i] = q.pop_back();
if (result[i] != int{})
++i;
}
};
std::memset(result, 0, sizeof result);
std::thread th1 (push_front);
std::thread th2 (pop_back);
th1.join();
th2.join();
for (size_t i=0 ; i<N ; ++i)
EXPECT_EQ (result[i], (int)i+1);
std::memset(result, 0, sizeof result);
std::thread th3 (push_back);
std::thread th4 (pop_front);
th3.join();
th4.join();
for (size_t i=0 ; i<N ; ++i)
EXPECT_EQ (result[i], (int)i+1);
}
}