]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/simplethread.h
ICU-57166.0.1.tar.gz
[apple/icu.git] / icuSources / test / intltest / simplethread.h
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2015, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7 #ifndef SIMPLETHREAD_H
8 #define SIMPLETHREAD_H
9
10 #include "mutex.h"
11
12 class U_EXPORT SimpleThread
13 {
14 public:
15 SimpleThread();
16 virtual ~SimpleThread();
17 int32_t start(void); // start the thread. Return 0 if successfull.
18 void join(); // A thread must be joined before deleting its SimpleThread.
19
20 virtual void run(void) = 0; // Override this to provide the code to run
21 // in the thread.
22 private:
23 void *fImplementation;
24 };
25
26
27 class IntlTest;
28
29 // ThreadPool - utililty class to simplify the spawning a group of threads by
30 // a multi-threaded test.
31 //
32 // Usage: from within an intltest test function,
33 // ThreadPool<TestClass> pool(
34 // this, // The current intltest test object,
35 // // of type "TestClass *"
36 // numberOfThreads, // How many threads to spawn.
37 // &TestClass::func); // The function to be run by each thread.
38 // // It takes one int32_t parameter which
39 // // is set to the thread number, 0 to numberOfThreads-1.
40 //
41 // pool.start(); // Start all threads running.
42 // pool.join(); // Wait until all threads have terminated.
43
44 class ThreadPoolBase {
45 public:
46 ThreadPoolBase(IntlTest *test, int32_t numThreads);
47 virtual ~ThreadPoolBase();
48
49 void start();
50 void join();
51
52 protected:
53 virtual void callFn(int32_t param) = 0;
54 friend class ThreadPoolThread;
55
56 IntlTest *fIntlTest;
57 int32_t fNumThreads;
58 SimpleThread **fThreads;
59 };
60
61
62 template<class TestClass>
63 class ThreadPool : public ThreadPoolBase {
64 private:
65 void (TestClass::*fRunFnPtr)(int32_t);
66 public:
67 ThreadPool(TestClass *test, int howMany, void (TestClass::*runFnPtr)(int32_t threadNumber)) :
68 ThreadPoolBase(test, howMany), fRunFnPtr(runFnPtr) {};
69 virtual ~ThreadPool() {};
70 private:
71 virtual void callFn(int32_t param) {
72 TestClass *test = dynamic_cast<TestClass *>(fIntlTest);
73 (test->*fRunFnPtr)(param);
74 }
75 };
76 #endif
77