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