1 ///////////////////////////////////////////////////////////////////////////
3 // Copyright (c) 2005, Industrial Light & Magic, a division of Lucas
6 // All rights reserved.
8 // Redistribution and use in source and binary forms, with or without
9 // modification, are permitted provided that the following conditions are
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of Industrial Light & Magic nor the names of
18 // its contributors may be used to endorse or promote products derived
19 // from this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ///////////////////////////////////////////////////////////////////////////
35 #ifndef INCLUDED_ILM_THREAD_H
36 #define INCLUDED_ILM_THREAD_H
38 //-----------------------------------------------------------------------------
42 // Class Thread is a portable interface to a system-dependent thread
43 // primitive. In order to make a thread actually do something useful,
44 // you must derive a subclass from class Thread and implement the
45 // run() function. If the operating system supports threading then
46 // the run() function will be executed int a new thread.
48 // The actual creation of the thread is done by the start() routine
49 // which then calls the run() function. In general the start()
50 // routine should be called from the constructor of the derived class.
52 // The base-class thread destructor will join/destroy the thread.
54 // IMPORTANT: Due to the mechanisms that encapsulate the low-level
55 // threading primitives in a C++ class there is a race condition
56 // with code resembling the following:
59 // WorkerThread myThread;
60 // } // myThread goes out of scope, is destroyed
61 // // and the thread is joined
63 // The race is between the parent thread joining the child thread
64 // in the destructor of myThread, and the run() function in the
65 // child thread. If the destructor gets executed first then run()
66 // will be called with an invalid "this" pointer.
68 // This issue can be fixed by using a Semaphore to keep track of
69 // whether the run() function has already been called. You can
70 // include a Semaphore member variable within your derived class
71 // which you post() on in the run() function, and wait() on in the
72 // destructor before the thread is joined. Alternatively you could
73 // do something like this:
75 // Semaphore runStarted;
77 // void WorkerThread::run ()
85 // WorkerThread myThread;
86 // runStarted.wait (); // ensure that we have started
87 // // the run function
88 // } // myThread goes out of scope, is destroyed
89 // // and the thread is joined
91 //-----------------------------------------------------------------------------
93 #include "OpenEXRConfig.h"
95 #if defined _WIN32 || defined _WIN64
106 namespace IlmThread {
109 // Query function to determine if the current platform supports
110 // threads AND this library was compiled with threading enabled.
113 bool supportsThreads ();
124 virtual void run () = 0;
128 #if defined _WIN32 || defined _WIN64
134 void operator = (const Thread& t); // not implemented
135 Thread (const Thread& t); // not implemented
139 } // namespace IlmThread