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 }
EIGEN_DECLARE_TEST
EIGEN_DECLARE_TEST(cxx11_tensor_notification)
Definition: cxx11_tensor_notification.cpp:60
VERIFY_IS_EQUAL
#define VERIFY_IS_EQUAL(a, b)
Definition: main.h:386
n
int n
Definition: BiCGSTAB_simple.cpp:1
test_notification_single
static void test_notification_single()
Definition: cxx11_tensor_notification.cpp:18
Eigen::ThreadPoolTempl::Schedule
void Schedule(std::function< void()> fn) EIGEN_OVERRIDE
Definition: NonBlockingThreadPool.h:101
Eigen::Notification
Definition: Barrier.h:61
test_notification_multiple
static void test_notification_multiple()
Definition: cxx11_tensor_notification.cpp:42
Eigen::ThreadPoolTempl
Definition: NonBlockingThreadPool.h:16
main.h
func
Definition: benchGeometry.cpp:23
CALL_SUBTEST
#define CALL_SUBTEST(FUNC)
Definition: main.h:399


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:09