diff --git a/Foundation/include/Poco/Thread.h b/Foundation/include/Poco/Thread.h index bfd6f031f..da8b43383 100644 --- a/Foundation/include/Poco/Thread.h +++ b/Foundation/include/Poco/Thread.h @@ -52,7 +52,7 @@ class Foundation_API Thread: private ThreadImpl /// Furthermore, a thread can be assigned a name. /// The name of a thread can be changed at any time. { -public: +public: typedef ThreadImpl::TIDImpl TID; using ThreadImpl::Callable; @@ -66,7 +66,7 @@ public: PRIO_HIGH = PRIO_HIGH_IMPL, /// A higher than normal thread priority. PRIO_HIGHEST = PRIO_HIGHEST_IMPL /// The highest thread priority. }; - + enum Policy { POLICY_DEFAULT = POLICY_DEFAULT_IMPL @@ -74,10 +74,10 @@ public: Thread(); /// Creates a thread. Call start() to start it. - + Thread(const std::string& name); /// Creates a named thread. Call start() to start it. - + ~Thread(); /// Destroys the thread. @@ -112,17 +112,17 @@ public: /// a scheduling policy can be specified. The policy is currently /// only used on POSIX platforms where the values SCHED_OTHER (default), /// SCHED_FIFO and SCHED_RR are supported. - + int getOSPriority() const; /// Returns the thread's priority, expressed as an operating system /// specific priority value. /// /// May return 0 if the priority has not been explicitly set. - + static int getMinOSPriority(int policy = POLICY_DEFAULT); /// Returns the minimum operating system-specific priority value, /// which can be passed to setOSPriority() for the given policy. - + static int getMaxOSPriority(int policy = POLICY_DEFAULT); /// Returns the maximum operating system-specific priority value, /// which can be passed to setOSPriority() for the given policy. @@ -144,6 +144,12 @@ public: /// valid during the entire lifetime of the thread, as /// only a reference to it is stored internally. + void start(Poco::SharedPtr pTarget); + /// Starts the thread with the given target. + /// + /// The Thread ensures that the given target stays + /// alive while the thread is running. + void start(Callable target, void* pData = 0); /// Starts the thread with the given target and parameter. @@ -155,15 +161,15 @@ public: } void join(); - /// Waits until the thread completes execution. + /// Waits until the thread completes execution. /// If multiple threads try to join the same /// thread, the result is undefined. - + void join(long milliseconds); /// Waits for at most the given interval for the thread /// to complete. Throws a TimeoutException if the thread /// does not complete within the specified time interval. - + bool tryJoin(long milliseconds); /// Waits for at most the given interval for the thread /// to complete. Returns true if the thread has finished, @@ -175,9 +181,9 @@ public: static bool trySleep(long milliseconds); /// Starts an interruptible sleep. When trySleep() is called, /// the thread will remain suspended until: - /// - the timeout expires or + /// - the timeout expires or /// - wakeUp() is called - /// + /// /// Function returns true if sleep attempt was completed, false /// if sleep was interrupted by a wakeUp() call. /// A frequent scenario where trySleep()/wakeUp() pair of functions @@ -185,13 +191,13 @@ public: /// with periodic activity between the idle times; trying to sleep /// (as opposed to sleeping) allows immediate ending of idle thread /// from the outside. - /// - /// The trySleep() and wakeUp() calls should be used with - /// understanding that the suspended state is not a true sleep, - /// but rather a state of waiting for an event, with timeout - /// expiration. This makes order of calls significant; calling - /// wakeUp() before calling trySleep() will prevent the next - /// trySleep() call to actually suspend the thread (which, in + /// + /// The trySleep() and wakeUp() calls should be used with + /// understanding that the suspended state is not a true sleep, + /// but rather a state of waiting for an event, with timeout + /// expiration. This makes order of calls significant; calling + /// wakeUp() before calling trySleep() will prevent the next + /// trySleep() call to actually suspend the thread (which, in /// some scenarios, may be desirable behavior). void wakeUp(); @@ -212,7 +218,7 @@ public: /// If the current thread is the main thread, 0 is returned. static TID currentTid(); - /// Returns the native thread ID for the current thread. + /// Returns the native thread ID for the current thread. protected: ThreadLocalStorage& tls(); @@ -223,7 +229,7 @@ protected: std::string makeName(); /// Creates a unique name for a thread. - + static int uniqueId(); /// Creates and returns a unique id for a thread. @@ -244,7 +250,7 @@ protected: { _functor(); } - + private: Functor _functor; }; @@ -282,7 +288,7 @@ inline int Thread::id() const inline std::string Thread::name() const { FastMutex::ScopedLock lock(_mutex); - + return _name; } @@ -290,7 +296,7 @@ inline std::string Thread::name() const inline std::string Thread::getName() const { FastMutex::ScopedLock lock(_mutex); - + return _name; } @@ -321,22 +327,22 @@ inline Thread* Thread::current() inline void Thread::setOSPriority(int prio, int policy) { - setOSPriorityImpl(prio, policy); + setOSPriorityImpl(prio, policy); } - + inline int Thread::getOSPriority() const { return getOSPriorityImpl(); } - + inline int Thread::getMinOSPriority(int policy) { return ThreadImpl::getMinOSPriorityImpl(policy); } - + inline int Thread::getMaxOSPriority(int policy) { return ThreadImpl::getMaxOSPriorityImpl(policy); diff --git a/Foundation/src/Thread.cpp b/Foundation/src/Thread.cpp index 2f16bbed7..0ebcba967 100644 --- a/Foundation/src/Thread.cpp +++ b/Foundation/src/Thread.cpp @@ -38,6 +38,7 @@ namespace Poco { namespace { + class RunnableHolder: public Runnable { public: @@ -87,18 +88,18 @@ private: } // namespace -Thread::Thread(): - _id(uniqueId()), - _name(makeName()), +Thread::Thread(): + _id(uniqueId()), + _name(makeName()), _pTLS(0), _event(true) { } -Thread::Thread(const std::string& name): - _id(uniqueId()), - _name(name), +Thread::Thread(const std::string& name): + _id(uniqueId()), + _name(name), _pTLS(0), _event(true) { @@ -129,6 +130,12 @@ void Thread::start(Runnable& target) } +void Thread::start(Poco::SharedPtr pTarget) +{ + startImpl(pTarget); +} + + void Thread::start(Callable target, void* pData) { startImpl(new CallableHolder(target, pData));