Lumiera  0.pre.03
»edit your freedom«
thread-wrapper-test.cpp
Go to the documentation of this file.
1 /*
2  ThreadWrapper(Test) - starting threads and passing context
3 
4  Copyright (C) Lumiera.org
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
28 #include "lib/test/run.hpp"
29 #include "lib/thread.hpp"
30 #include "lib/iter-explorer.hpp"
33 
34 #include <atomic>
35 #include <chrono>
36 
37 using test::Test;
38 using lib::explore;
39 using std::atomic_uint;
40 using std::this_thread::yield;
41 using std::this_thread::sleep_for;
42 using std::chrono::microseconds;
43 
44 
45 namespace lib {
46 namespace test{
47 
48  namespace { // test parameters
49 
50  const uint NUM_THREADS = 200;
51  const uint REPETITIONS = 10;
52  }
53 
54 
55  /*******************************************************************/
62  class ThreadWrapper_test : public Test
63  {
64 
65  virtual void
66  run (Arg)
67  {
70  }
71 
72 
74  void
76  {
77  atomic_uint i{0};
78  Thread thread("counter", [&]{ ++i; }); // bind a λ and launch thread
79  while (thread) yield(); // ensure thread has finished and detached
80 
81  CHECK (i == 1); // verify the effect has taken place
82  }
83 
84 
97  void
99  {
100  struct TestThread
101  : Thread
102  {
103  using Thread::Thread;
104 
105  uint local{0};
106 
107  void
108  doIt (uint a, uint b)
109  {
110  uint sum = a + b;
111  sleep_for (microseconds{sum}); // Note: explicit random delay before local store
112  local = sum;
113  }
114  };
115 
116  // prepare Storage for these objects (not created yet)
117  lib::ScopedCollection<TestThread> threads{NUM_THREADS};
118 
119  size_t checkSum = 0;
120  size_t globalSum = 0;
121  auto launchThreads = [&]
122  {
123  for (uint i=1; i<=NUM_THREADS; ++i)
124  {
125  uint x = rand() % 1000;
126  globalSum += (i + x);
127  threads.emplace (&TestThread::doIt, i, x);
128  } // Note: bind to member function, copying arguments
129 
130  while (explore(threads).has_any())
131  yield(); // wait for all threads to have detached
132 
133  for (auto& t : threads)
134  {
135  CHECK (0 < t.local);
136  checkSum += t.local;
137  }
138  };
139 
140  double runTime = benchmarkTime (launchThreads, REPETITIONS);
141 
142  CHECK (checkSum == globalSum); // sum of precomputed random numbers matches sum from threads
143  CHECK (runTime < NUM_THREADS * 1000/2); // random sleep time should be > 500ms on average
144  }
145  };
146 
147 
148 
150  LAUNCHER (ThreadWrapper_test, "function common");
151 
152 
153 
154 }} // namespace lib::test
A fixed collection of non-copyable polymorphic objects.
auto explore(IT &&srcSeq)
start building a IterExplorer by suitably wrapping the given iterable source.
Definition: run.hpp:49
Functions to perform (multithreaded) timing measurement on a given functor.
Implementation namespace for support and library code.
Managing a collection of non-copyable polymorphic objects in compact storage.
double benchmarkTime(FUN const &invokeTestCode, const size_t repeatCnt=1)
Helper to invoke a functor or λ to observe its running time.
Abstract Base Class for all testcases.
Definition: run.hpp:62
Simple test class runner.
Convenience front-end to simplify and codify basic thread handling.
A thin convenience wrapper to simplify thread-handling.
Definition: thread.hpp:656
Building tree expanding and backtracking evaluations within hierarchical scopes.