cxx11_tensor_notification.cpp
Go to the documentation of this file.
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 
19 {
20  ThreadPool thread_pool(1);
21 
22  std::atomic<int> counter(0);
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().
43 {
44  ThreadPool thread_pool(1);
45 
46  std::atomic<int> counter(0);
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 {
64 }
int n
void Notify()
Definition: Barrier.h:25
EIGEN_DECLARE_TEST(cxx11_tensor_notification)
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
static void test_notification_multiple()
static void test_notification_single()
void Wait()
Definition: Barrier.h:40
void Schedule(std::function< void()> fn) EIGEN_OVERRIDE
#define CALL_SUBTEST(FUNC)
Definition: main.h:399


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:08