File: simplethread.h

package info (click to toggle)
icu 78.2-1
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 123,992 kB
  • sloc: cpp: 527,891; ansic: 112,789; sh: 4,983; makefile: 4,657; perl: 3,199; python: 2,933; xml: 749; sed: 36; lisp: 12
file content (87 lines) | stat: -rw-r--r-- 2,926 bytes parent folder | download | duplicates (11)
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) 1997-2015, International Business Machines Corporation and
 * others. All Rights Reserved.
 ********************************************************************/

#ifndef SIMPLETHREAD_H
#define SIMPLETHREAD_H

#include <thread>
#include "unicode/utypes.h"

/*
 * Simple class for creating threads in ICU tests.
 * Originally created to provide a portable abstraction over platform
 * (POSIX or Win32) threading interfaces.
 *
 * New threaded tests should consider skipping this class and directly using C++ std library
 * threading functions. SimpleThread is retained primarily to support existing use.
 */
class SimpleThread
{
  public:
    SimpleThread();
    virtual  ~SimpleThread();
    int32_t   start();            // start the thread. Return 0 if successful.
    void      join();             // A thread must be joined before deleting its SimpleThread.

    virtual void run() = 0;       // Override this to provide the code to run
                                  //   in the thread.
  private:
    std::thread fThread = {};
};


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) override {
        TestClass *test = dynamic_cast<TestClass *>(fIntlTest);
        (test->*fRunFnPtr)(param);
    }
};
#endif