HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IlmThread.h
Go to the documentation of this file.
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright (c) Contributors to the OpenEXR Project.
4 //
5 
6 #ifndef INCLUDED_ILM_THREAD_H
7 #define INCLUDED_ILM_THREAD_H
8 
9 //-----------------------------------------------------------------------------
10 //
11 // class Thread
12 //
13 // Class Thread is a portable interface to a system-dependent thread
14 // primitive. In order to make a thread actually do something useful,
15 // you must derive a subclass from class Thread and implement the
16 // run() function. If the operating system supports threading then
17 // the run() function will be executed int a new thread.
18 //
19 // The actual creation of the thread is done by the start() routine
20 // which then calls the run() function. In general the start()
21 // routine should be called from the constructor of the derived class.
22 //
23 // The base-class thread destructor will join/destroy the thread.
24 //
25 // IMPORTANT: Due to the mechanisms that encapsulate the low-level
26 // threading primitives in a C++ class there is a race condition
27 // with code resembling the following:
28 //
29 // {
30 // WorkerThread myThread;
31 // } // myThread goes out of scope, is destroyed
32 // // and the thread is joined
33 //
34 // The race is between the parent thread joining the child thread
35 // in the destructor of myThread, and the run() function in the
36 // child thread. If the destructor gets executed first then run()
37 // will be called with an invalid "this" pointer.
38 //
39 // This issue can be fixed by using a Semaphore to keep track of
40 // whether the run() function has already been called. You can
41 // include a Semaphore member variable within your derived class
42 // which you post() on in the run() function, and wait() on in the
43 // destructor before the thread is joined. Alternatively you could
44 // do something like this:
45 //
46 // Semaphore runStarted;
47 //
48 // void WorkerThread::run ()
49 // {
50 // runStarted.post()
51 // // do some work
52 // ...
53 // }
54 //
55 // {
56 // WorkerThread myThread;
57 // runStarted.wait (); // ensure that we have started
58 // // the run function
59 // } // myThread goes out of scope, is destroyed
60 // // and the thread is joined
61 //
62 //-----------------------------------------------------------------------------
63 
64 #include "IlmThreadConfig.h"
65 #include "IlmThreadExport.h"
66 #include "IlmThreadNamespace.h"
67 
68 #if ILMTHREAD_THREADING_ENABLED
69 #include <thread>
70 #endif
71 
73 
74 //
75 // Query function to determine if the current platform supports
76 // threads AND this library was compiled with threading enabled.
77 //
78 
80 
81 
83 {
84  public:
85 
87  ILMTHREAD_EXPORT virtual ~Thread ();
88 
89  ILMTHREAD_EXPORT void start ();
90 
91  virtual void run () = 0;
92 
93  //
94  // wait for thread to exit - must be called before deleting thread
95  //
96  ILMTHREAD_EXPORT void join();
97  ILMTHREAD_EXPORT bool joinable() const;
98 
99  private:
100 
101 #if ILMTHREAD_THREADING_ENABLED
102  std::thread _thread;
103 #endif
104 
105  Thread &operator= (const Thread& t) = delete;
106  Thread &operator= (Thread&& t) = delete;
107  Thread (const Thread& t) = delete;
108  Thread (Thread&& t) = delete;
109 };
110 
111 
113 
114 #endif // INCLUDED_ILM_THREAD_H
#define ILMTHREAD_EXPORT
GLuint start
Definition: glcorearb.h:475
GLdouble GLdouble t
Definition: glew.h:1403
#define ILMTHREAD_INTERNAL_NAMESPACE_HEADER_ENTER
#define ILMTHREAD_INTERNAL_NAMESPACE_HEADER_EXIT
ILMTHREAD_INTERNAL_NAMESPACE_HEADER_ENTER ILMTHREAD_EXPORT bool supportsThreads()
#define ILMTHREAD_EXPORT_TYPE
**Note that the tasks the is the thread number *for the or if it s being executed by a non pool thread(this *can happen in cases where the whole pool is occupied and the calling *thread contributes to running the work load).**Thread pool.Have fun
auto join(It begin, Sentinel end, string_view sep) -> join_view< It, Sentinel >
Definition: format.h:2559