1 // Created on: 2006-03-10
2 // Created by: data exchange team
3 // Copyright (c) 2006-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef _OSD_Thread_HeaderFile
17 #define _OSD_Thread_HeaderFile
19 #include <Standard.hxx>
20 #include <Standard_DefineAlloc.hxx>
21 #include <Standard_Handle.hxx>
23 #include <OSD_ThreadFunction.hxx>
24 #include <OSD_PThread.hxx>
25 #include <Standard_ThreadId.hxx>
26 #include <Standard_Integer.hxx>
27 #include <Standard_Boolean.hxx>
28 #include <Standard_Address.hxx>
31 //! A simple platform-intependent interface to execute
32 //! and control threads.
40 Standard_EXPORT OSD_Thread();
42 //! Initialize the tool by the thread function
44 //! Note: On Windows, you might have to take an address of the thread
45 //! function explicitly to pass it to this constructor without compiler error
46 Standard_EXPORT OSD_Thread(const OSD_ThreadFunction& func);
49 Standard_EXPORT OSD_Thread(const OSD_Thread& other);
51 //! Copy thread handle from other OSD_Thread object.
52 Standard_EXPORT void Assign (const OSD_Thread& other);
53 void operator = (const OSD_Thread& other)
58 //! Destructor. Detaches the thread if it wasn't done already.
59 Standard_EXPORT ~OSD_Thread();
61 Standard_EXPORT void SetPriority (const Standard_Integer thePriority);
63 //! Initialize the tool by the thread function.
64 //! If the current thread handle is not null, nullifies it.
66 //! Note: On Windows, you might have to take an address of the thread
67 //! function explicitly to pass it to this method without compiler error
68 Standard_EXPORT void SetFunction (const OSD_ThreadFunction& func);
70 //! Starts a thread with thread function given in constructor,
71 //! passing the specified input data (as void *) to it.
72 //! The parameter \a WNTStackSize (on Windows only)
73 //! specifies size of the stack to be allocated for the thread
74 //! (by default - the same as for the current executable).
75 //! Returns True if thread started successfully
76 Standard_EXPORT Standard_Boolean Run (const Standard_Address data = 0, const Standard_Integer WNTStackSize = 0);
78 //! Detaches the execution thread from this Thread object,
79 //! so that it cannot be waited.
80 //! Note that mechanics of this operation is different on
81 //! UNIX/Linux (the thread is put to detached state) and Windows
82 //! (the handle is closed).
83 //! However, the purpose is the same: to instruct the system to
84 //! release all thread data upon its completion.
85 Standard_EXPORT void Detach();
87 //! Waits till the thread finishes execution.
88 Standard_Boolean Wait()
90 Standard_Address aRes = 0;
94 //! Wait till the thread finishes execution.
95 //! Returns True if wait was successful, False in case of error.
97 //! If successful and \a result argument is provided, saves the pointer
98 //! (void*) returned by the thread function in \a result.
100 //! Note however that it is advisable not to rely upon returned result
101 //! value, as it is not always the value actually returned by the thread
102 //! function. In addition, on Windows it is converted via DWORD.
103 Standard_EXPORT Standard_Boolean Wait (Standard_Address& theResult);
105 //! Waits for some time and if the thread is finished,
106 //! it returns the result.
107 //! The function returns false if the thread is not finished yet.
108 Standard_EXPORT Standard_Boolean Wait (const Standard_Integer time, Standard_Address& theResult);
110 //! Returns ID of the currently controlled thread ID,
111 //! or 0 if no thread is run
112 Standard_EXPORT Standard_ThreadId GetId() const;
114 //! Auxiliary: returns ID of the current thread
115 Standard_EXPORT static Standard_ThreadId Current();
119 OSD_ThreadFunction myFunc;
120 OSD_PThread myThread;
121 Standard_ThreadId myThreadId;
122 Standard_Integer myPriority;
126 #endif // _OSD_Thread_HeaderFile