]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/simplethread.h
1 /********************************************************************
3 * Copyright (c) 1997-2015, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
12 class U_EXPORT 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.
20 virtual void run(void) = 0; // Override this to provide the code to run
23 void *fImplementation
;
29 // ThreadPool - utililty class to simplify the spawning a group of threads by
30 // a multi-threaded test.
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.
41 // pool.start(); // Start all threads running.
42 // pool.join(); // Wait until all threads have terminated.
44 class ThreadPoolBase
{
46 ThreadPoolBase(IntlTest
*test
, int32_t numThreads
);
47 virtual ~ThreadPoolBase();
53 virtual void callFn(int32_t param
) = 0;
54 friend class ThreadPoolThread
;
58 SimpleThread
**fThreads
;
62 template<class TestClass
>
63 class ThreadPool
: public ThreadPoolBase
{
65 void (TestClass::*fRunFnPtr
)(int32_t);
67 ThreadPool(TestClass
*test
, int howMany
, void (TestClass::*runFnPtr
)(int32_t threadNumber
)) :
68 ThreadPoolBase(test
, howMany
), fRunFnPtr(runFnPtr
) {};
69 virtual ~ThreadPool() {};
71 virtual void callFn(int32_t param
) {
72 TestClass
*test
= dynamic_cast<TestClass
*>(fIntlTest
);
73 (test
->*fRunFnPtr
)(param
);