]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/simplethread.h
1 // © 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
13 #include "unicode/utypes.h"
16 * Simple class for creating threads in ICU tests.
17 * Originally created to provide a portable abstraction over platform
18 * (POSIX or Win32) threading interfaces.
20 * New threaded tests should consider skipping this class and directly using C++ std library
21 * threading functions. SimpleThread is retained primarily to support existing use.
27 virtual ~SimpleThread();
28 int32_t start(); // start the thread. Return 0 if successfull.
29 void join(); // A thread must be joined before deleting its SimpleThread.
31 virtual void run() = 0; // Override this to provide the code to run
34 std::thread fThread
= {};
40 // ThreadPool - utililty class to simplify the spawning a group of threads by
41 // a multi-threaded test.
43 // Usage: from within an intltest test function,
44 // ThreadPool<TestClass> pool(
45 // this, // The current intltest test object,
46 // // of type "TestClass *"
47 // numberOfThreads, // How many threads to spawn.
48 // &TestClass::func); // The function to be run by each thread.
49 // // It takes one int32_t parameter which
50 // // is set to the thread number, 0 to numberOfThreads-1.
52 // pool.start(); // Start all threads running.
53 // pool.join(); // Wait until all threads have terminated.
55 class ThreadPoolBase
{
57 ThreadPoolBase(IntlTest
*test
, int32_t numThreads
);
58 virtual ~ThreadPoolBase();
64 virtual void callFn(int32_t param
) = 0;
65 friend class ThreadPoolThread
;
69 SimpleThread
**fThreads
;
73 template<class TestClass
>
74 class ThreadPool
: public ThreadPoolBase
{
76 void (TestClass::*fRunFnPtr
)(int32_t);
78 ThreadPool(TestClass
*test
, int howMany
, void (TestClass::*runFnPtr
)(int32_t threadNumber
)) :
79 ThreadPoolBase(test
, howMany
), fRunFnPtr(runFnPtr
) {};
80 virtual ~ThreadPool() {};
82 virtual void callFn(int32_t param
) {
83 TestClass
*test
= dynamic_cast<TestClass
*>(fIntlTest
);
84 (test
->*fRunFnPtr
)(param
);