memgraph/tests/concurrent/spinlock.cpp
Teon Banek c7b6cae526 Extract io/network into mg-io library
Reviewers: buda, dgleich, mferencevic

Reviewed By: mferencevic

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D1411
2018-05-30 14:58:41 +02:00

41 lines
719 B
C++

#include <atomic>
#include <chrono>
#include <mutex>
#include <thread>
#include <vector>
#include "glog/logging.h"
#include "utils/thread/sync.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);
CHECK(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;
}