Polygon Crucher SDK - Documentation
Documentation
Loading...
Searching...
No Matches
XThread.h
1#if !defined(XTHREAD_H)
2#define XTHREAD_H
3
4#ifdef _MSC_VER
5#pragma once
6#endif // _MSC_VER
7
8#include "xosoperation.h"
9
10BEGIN_MOOTOOLS_NAMESPACE
11
13class DLL_TOOLSFUNCTION CXThread : public CXObject
14{
15 DECLARE_DYNAMIC_XOBJECT(CXThread)
16
17public:
18 CXThread(bool autoDelete = true); // Thread delete themselves, unless XTHREAD_PREVENT_AUTODELETE is set when creating the thread
19 CXThread(XTHREADPROC threadCallback, void *threadData);
20 virtual ~CXThread();
21
22 static void SleepCurrentThread(unsigned int milliSeconds);
23 static xThreadID GetMainThreadID();
24 static xThreadID GetCurrentThreadID();
25 static CXThread* BeginThread(CXThread *pThread, xThreadPriority = XTHREAD_PRIORITY_NORMAL, unsigned int createFlags = XTHREAD_DEFAULT, size_t stackSize = 0);
26 static CXThread* BeginThread(XTHREADPROC threadCallback, void *pUserThreadData, xThreadPriority = XTHREAD_PRIORITY_NORMAL, unsigned int createFlags = XTHREAD_DEFAULT, size_t stackSize = 0);
27 static CXThread *BeginThread(const CXRuntimeClass* pThreadClass, void *pUserThreadData = NULL, xThreadPriority = XTHREAD_PRIORITY_NORMAL, unsigned int createFlags = XTHREAD_DEFAULT, size_t stackSize = 0);
28
29 // Operations
30 xThreadHandle GetThreadHandle() const;
31 xThreadID GetThreadID() const;
32 bool IsValidThread() const; // return true if the thread is valid handle.
33 bool IsSuspended() const; // return true if the thread is suspended
34 bool IsTerminated() const; // Return true if the thread is terminated and can be safely deleted (case autodelete is off)
35
36 bool SuspendThread(); // Suspend a running thread only work on Windows OS
37 bool ResumeThread(); // Thread can be created suspended then being resumed after its creating
38 xThreadPriority GetThreadPriority() const;
39 bool SetThreadPriority(xThreadPriority priority); // Thread priority can be modified at runtime on Windows OS
40
41 bool ShouldCancel() const; // Return true if someone call to stop
42 void SetCancel();
43 bool Lock(bool lock) const; // Lock mechanism for accessing the thread from different part
44 void SetAutoDelete(bool autoDelete); // Change autodeletion thread status (safe threaded). If set, the caller must take care to do not call the thread externally as it can be deleted at any moment, once it finish its job.
45
46 void *GetUserThreadData(); // Return an user data defined at creation
47
48protected:
49 typedef enum ThreadFlags
50 {
51 XTHREAD_FLAGS_NONE = 0x00,
52 XTHREAD_FLAGS_AUTODELETE = 0x01, // The thread object, is automatically deleted
53 XTHREAD_FLAGS_SET_CANCEL = 0x02, // Ask the thread to cancel.
54 XTHREAD_FLAGS_IS_CALLBACK = 0x04, // The thread is a XTHREADPROC callback.
55 XTHREAD_FLAGS_IS_TERMINATED = 0x08, // The thread is terminated and can be safely deleted
56 XTHREAD_IS_NOT_CRITICAL = 0x10, // The thread can be hard terminated "safely"
57 } ThreadFlags;
58
59 bool AutoDelete(); // Delete only if autoDelete set, return true if deleted
60 void SetFlag(ThreadFlags flag, bool set);
61 bool IsFlagSet(ThreadFlags flag) const;
62
63 virtual bool Start(); // return false means initialization of the thread fails. Run is not called
64 virtual void Finish();
65 virtual bool Run();
66
67private:
68 XTHREADPROC userThreadCallback; // A callback defined by the user, if CXThread if not overidden (otherwise CXThread::Run is called)
69 void *userThreadData;
70
71 xThreadContext threadContext; // Handle of this thread, the handle is non null even after thread termination.
72 mutable CXCriticalSection *threadLock;
73 mutable unsigned int flags;
74
75 static bool StaticThreadCallback(void *data);
76};
77
78END_MOOTOOLS_NAMESPACE
79
80#endif // !defined(XTHREAD_H)
The class defines an x, y, z 3D point which can use int, float or double.
Definition 3DPoint.h:27
Definition XThreadSync.h:20
Definition XThread.h:14