memgraph/tests/concurrent/spinlock.cpp
Dominik Gleich fcecb14545 Replace debug_assert, permanent_assert with DCHECK/CHECK
Summary:
Phase 2.

Phase 3.

Phase 4.

Phase 5.

Complete refactor.

Reviewers: florijan, mislav.bradac

Reviewed By: mislav.bradac

Subscribers: mislav.bradac, pullbot

Differential Revision: https://phabricator.memgraph.io/D895
2017-10-11 14:43:32 +02:00

41 lines
711 B
C++

#include <atomic>
#include <chrono>
#include <mutex>
#include <thread>
#include <vector>
#include "glog/logging.h"
#include "threading/sync/spinlock.hpp"
int x = 0;
SpinLock lock;
void test_lock() {
using namespace std::literals;
{
std::unique_lock<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;
}