1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/********************************************************************
* COPYRIGHT:
* Copyright (c) 1999-2015, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
#include "simplethread.h"
#include <thread>
#include "unicode/utypes.h"
#include "intltest.h"
SimpleThread::SimpleThread() {
}
SimpleThread::~SimpleThread() {
this->join(); // Avoid crashes if user neglected to join().
}
int SimpleThread::start() {
fThread = std::thread(&SimpleThread::run, this);
return fThread.joinable() ? 0 : 1;
}
void SimpleThread::join() {
if (fThread.joinable()) {
fThread.join();
}
}
class ThreadPoolThread: public SimpleThread {
public:
ThreadPoolThread(ThreadPoolBase *pool, int32_t threadNum) : fPool(pool), fNum(threadNum) {}
virtual void run() override { fPool->callFn(fNum); }
ThreadPoolBase *fPool;
int32_t fNum;
};
ThreadPoolBase::ThreadPoolBase(IntlTest *test, int32_t howMany) :
fIntlTest(test), fNumThreads(howMany), fThreads(nullptr) {
fThreads = new SimpleThread *[fNumThreads];
if (fThreads == nullptr) {
fIntlTest->errln("%s:%d memory allocation failure.", __FILE__, __LINE__);
return;
}
for (int i=0; i<fNumThreads; i++) {
fThreads[i] = new ThreadPoolThread(this, i);
if (fThreads[i] == nullptr) {
fIntlTest->errln("%s:%d memory allocation failure.", __FILE__, __LINE__);
}
}
}
void ThreadPoolBase::start() {
for (int i=0; i<fNumThreads; i++) {
if (fThreads && fThreads[i]) {
fThreads[i]->start();
}
}
}
void ThreadPoolBase::join() {
for (int i=0; i<fNumThreads; i++) {
if (fThreads && fThreads[i]) {
fThreads[i]->join();
}
}
}
ThreadPoolBase::~ThreadPoolBase() {
if (fThreads) {
for (int i=0; i<fNumThreads; i++) {
delete fThreads[i];
fThreads[i] = nullptr;
}
delete[] fThreads;
fThreads = nullptr;
}
}
|