added start() method that takes a Poco::SharedPtr to a Runnable

This commit is contained in:
Günter Obiltschnig
2019-02-23 13:46:54 -05:00
parent e3e8c3474b
commit b961ea8148
2 changed files with 47 additions and 34 deletions

View File

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

View File

@@ -38,6 +38,7 @@ namespace Poco {
namespace { namespace {
class RunnableHolder: public Runnable class RunnableHolder: public Runnable
{ {
public: public:
@@ -87,18 +88,18 @@ private:
} // namespace } // namespace
Thread::Thread(): Thread::Thread():
_id(uniqueId()), _id(uniqueId()),
_name(makeName()), _name(makeName()),
_pTLS(0), _pTLS(0),
_event(true) _event(true)
{ {
} }
Thread::Thread(const std::string& name): Thread::Thread(const std::string& name):
_id(uniqueId()), _id(uniqueId()),
_name(name), _name(name),
_pTLS(0), _pTLS(0),
_event(true) _event(true)
{ {
@@ -129,6 +130,12 @@ void Thread::start(Runnable& target)
} }
void Thread::start(Poco::SharedPtr<Runnable> pTarget)
{
startImpl(pTarget);
}
void Thread::start(Callable target, void* pData) void Thread::start(Callable target, void* pData)
{ {
startImpl(new CallableHolder(target, pData)); startImpl(new CallableHolder(target, pData));