1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 1997-2015, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
10 #define SIMPLETHREAD_H
14 class U_EXPORT 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.
22 virtual void run(void) = 0; // Override this to provide the code to run
25 void *fImplementation;
31 // ThreadPool - utililty class to simplify the spawning a group of threads by
32 // a multi-threaded test.
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.
43 // pool.start(); // Start all threads running.
44 // pool.join(); // Wait until all threads have terminated.
46 class ThreadPoolBase {
48 ThreadPoolBase(IntlTest *test, int32_t numThreads);
49 virtual ~ThreadPoolBase();
55 virtual void callFn(int32_t param) = 0;
56 friend class ThreadPoolThread;
60 SimpleThread **fThreads;
64 template<class TestClass>
65 class ThreadPool : public ThreadPoolBase {
67 void (TestClass::*fRunFnPtr)(int32_t);
69 ThreadPool(TestClass *test, int howMany, void (TestClass::*runFnPtr)(int32_t threadNumber)) :
70 ThreadPoolBase(test, howMany), fRunFnPtr(runFnPtr) {};
71 virtual ~ThreadPool() {};
73 virtual void callFn(int32_t param) {
74 TestClass *test = dynamic_cast<TestClass *>(fIntlTest);
75 (test->*fRunFnPtr)(param);