Add operator support to collision guard
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <concepts>
|
||||
#include <shared_mutex>
|
||||
|
||||
namespace MultiThreading {
|
||||
@@ -33,7 +34,80 @@ public:
|
||||
public:
|
||||
Read operator->() const { return Read(mutex, value); }
|
||||
Write operator->() { return Write(mutex, value); }
|
||||
public:
|
||||
template <typename U = T>
|
||||
requires requires(const U& a, const U& b) { a + b; }
|
||||
T operator +(const T& rhs) const {
|
||||
std::shared_lock lock(mutex);
|
||||
return value + rhs;
|
||||
}
|
||||
|
||||
template <typename U = T>
|
||||
requires requires(U& a, const U& b) { a = b; }
|
||||
CollisionGuard& operator =(const T& rhs) {
|
||||
std::unique_lock lock(mutex);
|
||||
value = rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
explicit operator T() const requires std::copy_constructible<T> {
|
||||
std::shared_lock lock(mutex);
|
||||
return value;
|
||||
}
|
||||
|
||||
template <typename U = T>
|
||||
requires requires(U& a, const U& b) { a += b; }
|
||||
CollisionGuard& operator +=(const T& rhs) {
|
||||
std::unique_lock lock(mutex);
|
||||
value += rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename U = T>
|
||||
requires requires(U& a, const U& b) { a -= b; }
|
||||
CollisionGuard& operator -=(const T& rhs) {
|
||||
std::unique_lock lock(mutex);
|
||||
value -= rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename U = T>
|
||||
requires requires(U& a, const U& b) { a *= b; }
|
||||
CollisionGuard& operator *=(const T& rhs) {
|
||||
std::unique_lock lock(mutex);
|
||||
value *= rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename U = T>
|
||||
requires requires(U& a, const U& b) { a /= b; }
|
||||
CollisionGuard& operator /=(const T& rhs) {
|
||||
std::unique_lock lock(mutex);
|
||||
value /= rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename U = T>
|
||||
requires requires(const U& a, const U& b) { a - b; }
|
||||
T operator -(const T& rhs) const {
|
||||
std::shared_lock lock(mutex);
|
||||
return value - rhs;
|
||||
}
|
||||
|
||||
template <typename U = T>
|
||||
requires requires(const U& a, const U& b) { a * b; }
|
||||
T operator *(const T& rhs) const {
|
||||
std::shared_lock lock(mutex);
|
||||
return value * rhs;
|
||||
}
|
||||
|
||||
template <typename U = T>
|
||||
requires requires(const U& a, const U& b) { a / b; }
|
||||
T operator /(const T& rhs) const {
|
||||
std::shared_lock lock(mutex);
|
||||
return value / rhs;
|
||||
}
|
||||
public:
|
||||
template <typename... Args>
|
||||
explicit CollisionGuard(Args&&... args) : value(std::forward<Args>(args)...) {}
|
||||
};
|
13
main.cpp
13
main.cpp
@@ -7,10 +7,9 @@
|
||||
using namespace MultiThreading;
|
||||
|
||||
//void cb() { std::cout << "hi" << std::endl; }
|
||||
int32_t some_test_func(int32_t hello) {
|
||||
void some_test_func() {
|
||||
for (unsigned int i = 0; i < 1000000000; i++) {}
|
||||
std::cout << "task " << hello << " finishes." << std::endl;
|
||||
return rand();
|
||||
std::cout << "task finishes." << std::endl;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -44,16 +43,14 @@ int main() {
|
||||
|
||||
int main() {
|
||||
srand(time(nullptr));
|
||||
|
||||
int32_t task_completion_count = 0;
|
||||
CollisionGuard<int> task_completion_count(0);
|
||||
auto* thread_pool = new ThreadPool();
|
||||
|
||||
for (unsigned int i = 0; i < 128; i++) {
|
||||
auto some_task = Task<int32_t>::Create(([i] { return some_test_func(i); }), nullptr, &task_completion_count);
|
||||
auto some_task = Task<void>::Create([&task_completion_count]() { task_completion_count += 1; }, [&task_completion_count]() { task_completion_count += 1; });
|
||||
thread_pool->Enqueue(some_task);
|
||||
}
|
||||
|
||||
/// do stuff after the job.
|
||||
delete thread_pool;
|
||||
std::cout << "The returned random value was: " << task_completion_count << std::endl;
|
||||
std::cout << "The number of tasks run was " << (int) task_completion_count << std::endl;
|
||||
}
|
Reference in New Issue
Block a user