diff options
Diffstat (limited to 'unsupported/test/cxx11_tensor_notification.cpp')
-rw-r--r-- | unsupported/test/cxx11_tensor_notification.cpp | 39 |
1 files changed, 11 insertions, 28 deletions
diff --git a/unsupported/test/cxx11_tensor_notification.cpp b/unsupported/test/cxx11_tensor_notification.cpp index c946007b8..8e8165302 100644 --- a/unsupported/test/cxx11_tensor_notification.cpp +++ b/unsupported/test/cxx11_tensor_notification.cpp @@ -9,38 +9,21 @@ #define EIGEN_USE_THREADS +#include <atomic> + #include <stdlib.h> #include "main.h" #include <Eigen/CXX11/Tensor> -#if EIGEN_OS_WIN || EIGEN_OS_WIN64 -#include <windows.h> -void sleep(int seconds) { - Sleep(seconds*1000); -} -#else -#include <unistd.h> -#endif - - -namespace { - -void WaitAndAdd(Eigen::Notification* n, int* counter) { - n->Wait(); - *counter = *counter + 1; -} - -} // namespace - static void test_notification_single() { ThreadPool thread_pool(1); - int counter = 0; + std::atomic<int> counter(0); Eigen::Notification n; - std::function<void()> func = std::bind(&WaitAndAdd, &n, &counter); + auto func = [&n, &counter](){ n.Wait(); ++counter;}; thread_pool.Schedule(func); - sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // The thread should be waiting for the notification. VERIFY_IS_EQUAL(counter, 0); @@ -48,7 +31,7 @@ static void test_notification_single() // Unblock the thread n.Notify(); - sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // Verify the counter has been incremented VERIFY_IS_EQUAL(counter, 1); @@ -60,21 +43,21 @@ static void test_notification_multiple() { ThreadPool thread_pool(1); - int counter = 0; + std::atomic<int> counter(0); Eigen::Notification n; - std::function<void()> func = std::bind(&WaitAndAdd, &n, &counter); + auto func = [&n, &counter](){ n.Wait(); ++counter;}; thread_pool.Schedule(func); thread_pool.Schedule(func); thread_pool.Schedule(func); thread_pool.Schedule(func); - sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); VERIFY_IS_EQUAL(counter, 0); n.Notify(); - sleep(1); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); VERIFY_IS_EQUAL(counter, 4); } -void test_cxx11_tensor_notification() +EIGEN_DECLARE_TEST(cxx11_tensor_notification) { CALL_SUBTEST(test_notification_single()); CALL_SUBTEST(test_notification_multiple()); |