11 #define EIGEN_USE_THREADS 13 #include <Eigen/CXX11/ThreadPool> 18 #ifdef EIGEN_COMP_MSVC_STRICT 28 MaxSizeVector<EventCount::Waiter> waiters(1);
30 EventCount ec(waiters);
31 EventCount::Waiter&
w = waiters[0];
50 int val = val_.load(std::memory_order_relaxed);
53 VERIFY_LE(val, kQueueSize);
54 if (val == kQueueSize)
return false;
55 if (val_.compare_exchange_weak(val, val + 1, std::memory_order_relaxed))
61 int val = val_.load(std::memory_order_relaxed);
64 VERIFY_LE(val, kQueueSize);
65 if (val == 0)
return false;
66 if (val_.compare_exchange_weak(val, val - 1, std::memory_order_relaxed))
71 bool Empty() {
return val_.load(std::memory_order_relaxed) == 0; }
81 const int kThreads = std::thread::hardware_concurrency();
82 static const int kEvents = 1 << 16;
83 static const int kQueues = 10;
85 MaxSizeVector<EventCount::Waiter> waiters(kThreads);
86 waiters.resize(kThreads);
87 EventCount ec(waiters);
90 std::vector<std::unique_ptr<std::thread>> producers;
91 for (
int i = 0; i < kThreads; i++) {
92 producers.emplace_back(
new std::thread([&ec, &queues]() {
93 unsigned int rnd =
static_cast<unsigned int>(std::hash<std::thread::id>()(std::this_thread::get_id()));
94 for (
int j = 0; j < kEvents; j++) {
96 if (queues[idx].
Push()) {
106 std::vector<std::unique_ptr<std::thread>> consumers;
107 for (
int i = 0; i < kThreads; i++) {
108 consumers.emplace_back(
new std::thread([&ec, &queues, &waiters, i]() {
109 EventCount::Waiter&
w = waiters[i];
110 unsigned int rnd =
static_cast<unsigned int>(std::hash<std::thread::id>()(std::this_thread::get_id()));
111 for (
int j = 0; j < kEvents; j++) {
113 if (queues[idx].
Pop())
continue;
117 for (
int q = 0;
q < kQueues;
q++) {
132 for (
int i = 0; i < kThreads; i++) {
133 producers[i]->join();
134 consumers[i]->join();
#define EIGEN_THREAD_YIELD()
static const int kQueueSize
EIGEN_DEVICE_FUNC const Scalar & q
void test_cxx11_eventcount()
TFSIMD_FORCE_INLINE const tfScalar & w() const
int rand_reentrant(unsigned int *s)
static void test_basic_eventcount()
#define EIGEN_UNUSED_VARIABLE(var)
static void test_stress_eventcount()