memgraph/tests/concurrent/spin_lock.cpp
antonio2368 28413fd626 Change log library to spdlog, expose log levels to user (#72)
* Change from glog to spdlog

* Remove HA tests

* Remove logrotate log configuration

* Define custom main for unit gtests
2021-01-21 16:30:55 +01:00

40 lines
700 B
C++

#include <atomic>
#include <chrono>
#include <mutex>
#include <thread>
#include <vector>
#include "utils/spin_lock.hpp"
int x = 0;
utils::SpinLock lock;
void test_lock() {
using namespace std::literals;
{
std::unique_lock<utils::SpinLock> guard(lock);
x++;
std::this_thread::sleep_for(25ms);
MG_ASSERT(x < 2,
"x always has to be less than 2 (other "
"threads shouldn't be able to change the x simultaneously");
x--;
}
}
int main() {
constexpr int N = 16;
std::vector<std::thread> threads;
for (int i = 0; i < N; ++i) threads.push_back(std::thread(test_lock));
for (auto &thread : threads) {
thread.join();
}
return 0;
}