cart-elc

Source code for CART-ELC
git clone git://git.laack.co/cart-elc.git
Log | Files | Refs | README | LICENSE

cxx11_tensor_notification.cpp (1789B)


      1 // This file is part of Eigen, a lightweight C++ template library
      2 // for linear algebra.
      3 //
      4 // Copyright (C) 2015 Vijay Vasudevan <vrv@google.com>
      5 //
      6 // This Source Code Form is subject to the terms of the Mozilla
      7 // Public License v. 2.0. If a copy of the MPL was not distributed
      8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
      9 
     10 #define EIGEN_USE_THREADS
     11 
     12 #include <atomic>
     13 
     14 #include <stdlib.h>
     15 #include "main.h"
     16 #include <Eigen/CXX11/Tensor>
     17 
     18 static void test_notification_single()
     19 {
     20   ThreadPool thread_pool(1);
     21 
     22   std::atomic<int> counter(0);
     23   Eigen::Notification n;
     24   auto func = [&n, &counter](){ n.Wait(); ++counter;};
     25   thread_pool.Schedule(func);
     26   std::this_thread::sleep_for(std::chrono::milliseconds(1000));
     27 
     28   // The thread should be waiting for the notification.
     29   VERIFY_IS_EQUAL(counter, 0);
     30 
     31   // Unblock the thread
     32   n.Notify();
     33 
     34   std::this_thread::sleep_for(std::chrono::milliseconds(1000));
     35 
     36   // Verify the counter has been incremented
     37   VERIFY_IS_EQUAL(counter, 1);
     38 }
     39 
     40 // Like test_notification_single() but enqueues multiple threads to
     41 // validate that all threads get notified by Notify().
     42 static void test_notification_multiple()
     43 {
     44   ThreadPool thread_pool(1);
     45 
     46   std::atomic<int> counter(0);
     47   Eigen::Notification n;
     48   auto func = [&n, &counter](){ n.Wait(); ++counter;};
     49   thread_pool.Schedule(func);
     50   thread_pool.Schedule(func);
     51   thread_pool.Schedule(func);
     52   thread_pool.Schedule(func);
     53   std::this_thread::sleep_for(std::chrono::milliseconds(1000));
     54   VERIFY_IS_EQUAL(counter, 0);
     55   n.Notify();
     56   std::this_thread::sleep_for(std::chrono::milliseconds(1000));
     57   VERIFY_IS_EQUAL(counter, 4);
     58 }
     59 
     60 EIGEN_DECLARE_TEST(cxx11_tensor_notification)
     61 {
     62   CALL_SUBTEST(test_notification_single());
     63   CALL_SUBTEST(test_notification_multiple());
     64 }