Thread.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // Thread.h
  3. //
  4. // $Id: //poco/Main/Foundation/include/Poco/Thread.h#3 $
  5. //
  6. // Library: Foundation
  7. // Package: Threading
  8. // Module: Thread
  9. //
  10. // Definition of the Thread class.
  11. //
  12. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // Permission is hereby granted, free of charge, to any person or organization
  16. // obtaining a copy of the software and accompanying documentation covered by
  17. // this license (the "Software") to use, reproduce, display, distribute,
  18. // execute, and transmit the Software, and to prepare derivative works of the
  19. // Software, and to permit third-parties to whom the Software is furnished to
  20. // do so, all subject to the following:
  21. //
  22. // The copyright notices in the Software and this entire statement, including
  23. // the above license grant, this restriction and the following disclaimer,
  24. // must be included in all copies of the Software, in whole or in part, and
  25. // all derivative works of the Software, unless such copies or derivative
  26. // works are solely in the form of machine-executable object code generated by
  27. // a source language processor.
  28. //
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  32. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  33. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  34. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  35. // DEALINGS IN THE SOFTWARE.
  36. //
  37. #ifndef Foundation_Thread_INCLUDED
  38. #define Foundation_Thread_INCLUDED
  39. #include "Poco/Foundation.h"
  40. #if defined(POCO_OS_FAMILY_WINDOWS)
  41. #include "Poco/Thread_WIN32.h"
  42. #else
  43. #include "Poco/Thread_POSIX.h"
  44. #endif
  45. namespace Poco {
  46. class Runnable;
  47. class ThreadLocalStorage;
  48. class Foundation_API Thread: private ThreadImpl
  49. /// This class implements a platform-independent
  50. /// wrapper to an operating system thread.
  51. ///
  52. /// Every Thread object gets a unique (within
  53. /// its process) numeric thread ID.
  54. /// Furthermore, a thread can be assigned a name.
  55. {
  56. public:
  57. enum Priority
  58. /// Thread priorities.
  59. {
  60. PRIO_LOWEST = PRIO_LOWEST_IMPL, /// The lowest thread priority.
  61. PRIO_LOW = PRIO_LOW_IMPL, /// A lower than normal thread priority.
  62. PRIO_NORMAL = PRIO_NORMAL_IMPL, /// The normal thread priority.
  63. PRIO_HIGH = PRIO_HIGH_IMPL, /// A higher than normal thread priority.
  64. PRIO_HIGHEST = PRIO_HIGHEST_IMPL /// The highest thread priority.
  65. };
  66. Thread();
  67. /// Creates a thread. Call start() to start it.
  68. Thread(const std::string& name);
  69. /// Creates a named thread. Call start() to start it.
  70. ~Thread();
  71. /// Destroys the thread.
  72. int id() const;
  73. /// Returns the unique thread ID of the thread.
  74. const std::string& name() const;
  75. /// Returns the name of the thread.
  76. void setPriority(Priority prio);
  77. /// Sets the thread's priority.
  78. ///
  79. /// Some platform only allow changing a thread's priority
  80. /// if the process has certain privileges.
  81. Priority getPriority() const;
  82. /// Returns the thread's priority.
  83. void start(Runnable& target);
  84. /// Starts the thread with the given target.
  85. void join();
  86. /// Waits until the thread completes execution.
  87. /// If multiple threads try to join the same
  88. /// thread, the result is undefined.
  89. void join(long milliseconds);
  90. /// Waits for at most the given interval for the thread
  91. /// to complete. Throws a TimeoutException if the thread
  92. /// does not complete within the specified time interval.
  93. bool tryJoin(long milliseconds);
  94. /// Waits for at most the given interval for the thread
  95. /// to complete. Returns true if the thread has finished,
  96. /// false otherwise.
  97. bool isRunning() const;
  98. /// Returns true if the thread is running.
  99. static void sleep(long milliseconds);
  100. /// Suspends the current thread for the specified
  101. /// amount of time.
  102. static void yield();
  103. /// Yields cpu to other threads.
  104. static Thread* current();
  105. /// Returns the Thread object for the currently active thread.
  106. /// If the current thread is the main thread, 0 is returned.
  107. protected:
  108. ThreadLocalStorage& tls();
  109. /// Returns a reference to the thread's local storage.
  110. void clearTLS();
  111. /// Clears the thread's local storage.
  112. std::string makeName();
  113. /// Creates a unique name for a thread.
  114. static int uniqueId();
  115. /// Creates and returns a unique id for a thread.
  116. void setName(const std::string& name);
  117. /// Sets the name of the thread.
  118. private:
  119. Thread(const Thread&);
  120. Thread& operator = (const Thread&);
  121. int _id;
  122. std::string _name;
  123. ThreadLocalStorage* _pTLS;
  124. friend class ThreadLocalStorage;
  125. friend class PooledThread;
  126. };
  127. //
  128. // inlines
  129. //
  130. inline int Thread::id() const
  131. {
  132. return _id;
  133. }
  134. inline const std::string& Thread::name() const
  135. {
  136. return _name;
  137. }
  138. inline bool Thread::isRunning() const
  139. {
  140. return isRunningImpl();
  141. }
  142. inline void Thread::sleep(long milliseconds)
  143. {
  144. sleepImpl(milliseconds);
  145. }
  146. inline void Thread::yield()
  147. {
  148. yieldImpl();
  149. }
  150. inline Thread* Thread::current()
  151. {
  152. return static_cast<Thread*>(currentImpl());
  153. }
  154. } // namespace Poco
  155. #endif // Foundation_Thread_INCLUDED