2009-10-05 20:57:57 +00:00
|
|
|
/********************************************************************
|
|
|
|
* COPYRIGHT:
|
2015-04-15 22:43:15 +00:00
|
|
|
* Copyright (c) 1997-2015, International Business Machines Corporation and
|
2009-10-05 20:57:57 +00:00
|
|
|
* others. All Rights Reserved.
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
#ifndef SIMPLETHREAD_H
|
|
|
|
#define SIMPLETHREAD_H
|
|
|
|
|
|
|
|
#include "mutex.h"
|
|
|
|
|
|
|
|
class U_EXPORT SimpleThread
|
|
|
|
{
|
2015-04-23 23:59:24 +00:00
|
|
|
public:
|
2009-10-05 20:57:57 +00:00
|
|
|
SimpleThread();
|
|
|
|
virtual ~SimpleThread();
|
2015-04-23 23:59:24 +00:00
|
|
|
int32_t start(void); // start the thread. Return 0 if successfull.
|
|
|
|
void join(); // A thread must be joined before deleting its SimpleThread.
|
2009-10-05 20:57:57 +00:00
|
|
|
|
|
|
|
virtual void run(void) = 0; // Override this to provide the code to run
|
|
|
|
// in the thread.
|
2015-04-23 23:59:24 +00:00
|
|
|
private:
|
2009-10-05 20:57:57 +00:00
|
|
|
void *fImplementation;
|
|
|
|
};
|
|
|
|
|
2015-12-03 23:10:38 +00:00
|
|
|
|
|
|
|
class IntlTest;
|
|
|
|
|
|
|
|
// ThreadPool - utililty class to simplify the spawning a group of threads by
|
|
|
|
// a multi-threaded test.
|
|
|
|
//
|
|
|
|
// Usage: from within an intltest test function,
|
|
|
|
// ThreadPool<TestClass> pool(
|
|
|
|
// this, // The current intltest test object,
|
|
|
|
// // of type "TestClass *"
|
|
|
|
// numberOfThreads, // How many threads to spawn.
|
|
|
|
// &TestClass::func); // The function to be run by each thread.
|
|
|
|
// // It takes one int32_t parameter which
|
|
|
|
// // is set to the thread number, 0 to numberOfThreads-1.
|
|
|
|
//
|
|
|
|
// pool.start(); // Start all threads running.
|
|
|
|
// pool.join(); // Wait until all threads have terminated.
|
|
|
|
|
|
|
|
class ThreadPoolBase {
|
|
|
|
public:
|
|
|
|
ThreadPoolBase(IntlTest *test, int32_t numThreads);
|
|
|
|
virtual ~ThreadPoolBase();
|
|
|
|
|
|
|
|
void start();
|
|
|
|
void join();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
virtual void callFn(int32_t param) = 0;
|
|
|
|
friend class ThreadPoolThread;
|
|
|
|
|
|
|
|
IntlTest *fIntlTest;
|
|
|
|
int32_t fNumThreads;
|
|
|
|
SimpleThread **fThreads;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
template<class TestClass>
|
|
|
|
class ThreadPool : public ThreadPoolBase {
|
|
|
|
private:
|
|
|
|
void (TestClass::*fRunFnPtr)(int32_t);
|
|
|
|
public:
|
|
|
|
ThreadPool(TestClass *test, int howMany, void (TestClass::*runFnPtr)(int32_t threadNumber)) :
|
|
|
|
ThreadPoolBase(test, howMany), fRunFnPtr(runFnPtr) {};
|
|
|
|
virtual ~ThreadPool() {};
|
|
|
|
private:
|
|
|
|
virtual void callFn(int32_t param) {
|
|
|
|
TestClass *test = dynamic_cast<TestClass *>(fIntlTest);
|
|
|
|
(test->*fRunFnPtr)(param);
|
|
|
|
}
|
|
|
|
};
|
2009-10-05 20:57:57 +00:00
|
|
|
#endif
|
|
|
|
|