- more threading things
TODO: documentation
This commit is contained in:
@@ -33,13 +33,62 @@
|
||||
#define FENNEC_TEST_THREADING_H
|
||||
|
||||
#include <fennec/threading/atomic.h>
|
||||
#include <fennec/threading/lock_guard.h>
|
||||
#include <fennec/threading/mutex.h>
|
||||
#include <fennec/threading/thread.h>
|
||||
|
||||
namespace fennec::test
|
||||
{
|
||||
|
||||
inline void fennec_test_threading_test_atomic(atomic<size_t>* test_atomic, size_t N = 1000) {
|
||||
for (size_t i = 0; i < N; ++i) {
|
||||
++*test_atomic;
|
||||
}
|
||||
}
|
||||
|
||||
template<size_t ThreadsV>
|
||||
inline void fennec_test_threading_run_test_atomic(array<thread, ThreadsV>& threads, size_t N = 1000) {
|
||||
atomic<size_t> test = 0;
|
||||
for (thread& t : threads) {
|
||||
t = thread(fennec_test_threading_test_atomic, &test, N);
|
||||
}
|
||||
for (thread& t : threads) {
|
||||
t.join();
|
||||
}
|
||||
fennec_test_run(test.load(), N * ThreadsV);
|
||||
}
|
||||
|
||||
|
||||
|
||||
inline void fennec_test_threading_test_mutex(size_t* var, mutex* m, size_t N = 1000) {
|
||||
for (size_t i = 0; i < N; ++i) {
|
||||
lock_guard guard(*m);
|
||||
++*var;
|
||||
}
|
||||
}
|
||||
|
||||
template<size_t ThreadsV>
|
||||
inline void fennec_test_threading_run_test_mutex(array<thread, ThreadsV>& threads, size_t N = 1000) {
|
||||
mutex m;
|
||||
size_t test = 0;
|
||||
for (thread& t : threads) {
|
||||
t = thread(fennec_test_threading_test_mutex, &test, &m, N);
|
||||
}
|
||||
for (thread& t : threads) {
|
||||
t.join();
|
||||
}
|
||||
fennec_test_run(test, N * ThreadsV);
|
||||
}
|
||||
|
||||
|
||||
inline void fennec_test_threading() {
|
||||
|
||||
atomic<bool> test;
|
||||
static constexpr size_t N = 1000, threads = 4;
|
||||
|
||||
array<thread, threads> arr;
|
||||
|
||||
fennec_test_threading_run_test_atomic(arr, N);
|
||||
fennec_test_threading_run_test_mutex(arr, N);
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user