]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/simplethread.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 1999-2015, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
10 #include "simplethread.h"
13 #include "unicode/utypes.h"
17 SimpleThread::SimpleThread() {
20 SimpleThread::~SimpleThread() {
21 this->join(); // Avoid crashes if user neglected to join().
24 int SimpleThread::start() {
25 fThread
= std::thread(&SimpleThread::run
, this);
26 return fThread
.joinable() ? 0 : 1;
29 void SimpleThread::join() {
30 if (fThread
.joinable()) {
37 class ThreadPoolThread
: public SimpleThread
{
39 ThreadPoolThread(ThreadPoolBase
*pool
, int32_t threadNum
) : fPool(pool
), fNum(threadNum
) {}
40 virtual void run() {fPool
->callFn(fNum
); }
41 ThreadPoolBase
*fPool
;
46 ThreadPoolBase::ThreadPoolBase(IntlTest
*test
, int32_t howMany
) :
47 fIntlTest(test
), fNumThreads(howMany
), fThreads(NULL
) {
48 fThreads
= new SimpleThread
*[fNumThreads
];
49 if (fThreads
== NULL
) {
50 fIntlTest
->errln("%s:%d memory allocation failure.", __FILE__
, __LINE__
);
54 for (int i
=0; i
<fNumThreads
; i
++) {
55 fThreads
[i
] = new ThreadPoolThread(this, i
);
56 if (fThreads
[i
] == NULL
) {
57 fIntlTest
->errln("%s:%d memory allocation failure.", __FILE__
, __LINE__
);
62 void ThreadPoolBase::start() {
63 for (int i
=0; i
<fNumThreads
; i
++) {
64 if (fThreads
&& fThreads
[i
]) {
70 void ThreadPoolBase::join() {
71 for (int i
=0; i
<fNumThreads
; i
++) {
72 if (fThreads
&& fThreads
[i
]) {
78 ThreadPoolBase::~ThreadPoolBase() {
80 for (int i
=0; i
<fNumThreads
; i
++) {