improved Lambda support

This commit is contained in:
Günter Obiltschnig
2020-01-26 08:41:22 +01:00
parent 14e58b7fea
commit 958387b6ba
6 changed files with 122 additions and 43 deletions

View File

@@ -154,12 +154,19 @@ public:
/// Starts the thread with the given target and parameter.
template <class Functor>
void startFunc(Functor fn)
void startFunc(const Functor& fn)
/// Starts the thread with the given functor object or lambda.
{
startImpl(new FunctorRunnable<Functor>(fn));
}
template <class Functor>
void startFunc(Functor&& fn)
/// Starts the thread with the given functor object or lambda.
{
startImpl(new FunctorRunnable<Functor>(std::move(fn)));
}
void join();
/// Waits until the thread completes execution.
/// If multiple threads try to join the same
@@ -242,6 +249,11 @@ protected:
{
}
FunctorRunnable(Functor&& functor):
_functor(std::move(functor))
{
}
~FunctorRunnable()
{
}

View File

@@ -36,7 +36,7 @@ public:
MyRunnable(): _ran(false)
{
}
void run()
{
Thread* pThread = Thread::current();
@@ -45,22 +45,22 @@ public:
_ran = true;
_event.wait();
}
bool ran() const
{
return _ran;
}
const std::string& threadName() const
{
return _threadName;
}
void notify()
{
_event.set();
}
static void staticFunc()
{
++_staticVar;
@@ -96,17 +96,17 @@ public:
NonJoinRunnable() : _finished(false)
{
}
void run()
{
_finished = true;
}
bool finished() const
{
return _finished;
}
private:
bool _finished;
};
@@ -264,12 +264,12 @@ void ThreadTest::testNotJoin()
Thread thread;
NonJoinRunnable r;
thread.start(r);
while (!r.finished())
{
Thread::sleep(10);
}
Thread::sleep(100);
assertTrue (!thread.isRunning());
}
@@ -383,20 +383,19 @@ void ThreadTest::testThreadFunctor()
assertTrue (!thread.isRunning());
#if __cplusplus >= 201103L
Thread thread2;
assertTrue (!thread2.isRunning());
MyRunnable::_staticVar = 0;
thread.startFunc([] () {MyRunnable::_staticVar++;});
thread.startFunc([] ()
{
MyRunnable::_staticVar++;
});
thread.join();
assertTrue (1 == MyRunnable::_staticVar);
assertTrue (!thread2.isRunning());
#endif
}

View File

@@ -30,9 +30,9 @@ namespace Util {
class Util_API Timer: protected Poco::Runnable
/// A Timer allows to schedule tasks (TimerTask objects) for future execution
/// in a background thread. Tasks may be scheduled for one-time execution,
/// or for repeated execution at regular intervals.
/// A Timer allows to schedule tasks (TimerTask objects) for future execution
/// in a background thread. Tasks may be scheduled for one-time execution,
/// or for repeated execution at regular intervals.
///
/// The Timer object creates a thread that executes all scheduled tasks
/// sequentially. Therefore, tasks should complete their work as quickly
@@ -41,20 +41,29 @@ class Util_API Timer: protected Poco::Runnable
/// Timer is safe for multithreaded use - multiple threads can schedule
/// new tasks simultaneously.
///
/// Via the func() helper function template, a functor or
/// lambda can be used as timer task:
///
/// timer.schedule(Timer::func([]()
/// {
/// std::cout << "Timer!\n";
/// }),
/// Poco::Clock());
///
/// Acknowledgement: The interface of this class has been inspired by
/// the java.util.Timer class from Java 1.3.
{
public:
Timer();
/// Creates the Timer.
explicit Timer(Poco::Thread::Priority priority);
/// Creates the Timer, using a timer thread with
/// the given priority.
~Timer();
/// Destroys the Timer, cancelling all pending tasks.
void cancel(bool wait = false);
/// Cancels all pending tasks.
///
@@ -65,7 +74,7 @@ public:
/// task queue will be purged as soon as the currently
/// running task finishes. If wait is true, waits
/// until the queue has been purged.
void schedule(TimerTask::Ptr pTask, Poco::Timestamp time);
/// Schedules a task for execution at the specified time.
///
@@ -84,7 +93,7 @@ public:
///
/// If the time lies in the past, the task is executed
/// immediately.
void schedule(TimerTask::Ptr pTask, long delay, long interval);
/// Schedules a task for periodic execution.
///
@@ -112,12 +121,12 @@ public:
/// The task is first executed at the given time.
/// Subsequently, the task is executed periodically with
/// the given interval in milliseconds between invocations.
void scheduleAtFixedRate(TimerTask::Ptr pTask, long delay, long interval);
/// Schedules a task for periodic execution at a fixed rate.
///
/// The task is first executed after the given delay.
/// Subsequently, the task is executed periodically
/// Subsequently, the task is executed periodically
/// every number of milliseconds specified by interval.
///
/// If task execution takes longer than the given interval,
@@ -127,7 +136,7 @@ public:
/// Schedules a task for periodic execution at a fixed rate.
///
/// The task is first executed at the given time.
/// Subsequently, the task is executed periodically
/// Subsequently, the task is executed periodically
/// every number of milliseconds specified by interval.
///
/// If task execution takes longer than the given interval,
@@ -144,20 +153,36 @@ public:
/// Schedules a task for periodic execution at a fixed rate.
///
/// The task is first executed at the given time.
/// Subsequently, the task is executed periodically
/// Subsequently, the task is executed periodically
/// every number of milliseconds specified by interval.
///
/// If task execution takes longer than the given interval,
/// further executions are delayed.
template <typename Fn>
static TimerTask::Ptr func(const Fn& fn)
/// Helper function template to use a functor or lambda
/// with Timer::schedule() and Timer::scheduleAtFixedRate().
{
return new TimerFunc<Fn>(fn);
}
template <typename Fn>
static TimerTask::Ptr func(Fn&& fn)
/// Helper function template to use a functor or lambda
/// with Timer::schedule() and Timer::scheduleAtFixedRate().
{
return new TimerFunc<Fn>(std::move(fn));
}
protected:
void run();
static void validateTask(const TimerTask::Ptr& pTask);
private:
Timer(const Timer&);
Timer& operator = (const Timer&);
Poco::TimedNotificationQueue _queue;
Poco::Thread _thread;
};

View File

@@ -30,7 +30,7 @@ namespace Util {
class Util_API TimerTask: public Poco::RefCountedObject, public Poco::Runnable
/// A task that can be scheduled for one-time or
/// A task that can be scheduled for one-time or
/// repeated execution by a Timer.
///
/// This is an abstract class. Subclasses must override the run() member
@@ -38,45 +38,71 @@ class Util_API TimerTask: public Poco::RefCountedObject, public Poco::Runnable
{
public:
using Ptr = Poco::AutoPtr<TimerTask>;
TimerTask();
/// Creates the TimerTask.
void cancel();
/// Cancels the execution of the timer.
/// If the task has been scheduled for one-time execution and has
/// not yet run, or has not yet been scheduled, it will never run.
/// If the task has been scheduled for repeated execution, it will never
/// run again. If the task is running when this call occurs, the task
/// If the task has been scheduled for one-time execution and has
/// not yet run, or has not yet been scheduled, it will never run.
/// If the task has been scheduled for repeated execution, it will never
/// run again. If the task is running when this call occurs, the task
/// will run to completion, but will never run again.
///
/// Warning: A TimerTask that has been cancelled must not be scheduled again.
/// An attempt to do so results in a Poco::Util::IllegalStateException being thrown.
bool isCancelled() const;
/// Returns true iff the TimerTask has been cancelled by a call
/// to cancel().
Poco::Timestamp lastExecution() const;
/// Returns the time of the last execution of the timer task.
///
/// Returns 0 if the timer has never been executed.
protected:
~TimerTask();
/// Destroys the TimerTask.
private:
TimerTask(const TimerTask&);
TimerTask& operator = (const TimerTask&);
Poco::Timestamp _lastExecution;
bool _isCancelled;
friend class TaskNotification;
};
template <typename Fn>
class TimerFunc: public TimerTask
/// A simple adapter that allows using a functor or lambda
/// with Poco::Util::Timer, used by timerFunc().
{
public:
explicit TimerFunc(const Fn& fn):
_fn(fn)
{
}
explicit TimerFunc(Fn&& fn):
_fn(std::move(fn))
{
}
void run()
{
_fn();
}
private:
Fn _fn;
};
//
// inlines
//

View File

@@ -260,6 +260,21 @@ void TimerTest::testCancelAllWaitStop()
}
void TimerTest::testFunc()
{
Timer timer;
int count = 0;
timer.schedule(Timer::func([&count]()
{
count++;
}), Poco::Clock());
Poco::Thread::sleep(100);
assertTrue (count == 1);
}
void TimerTest::setUp()
{
}
@@ -290,6 +305,7 @@ CppUnit::Test* TimerTest::suite()
CppUnit_addTest(pSuite, TimerTest, testCancel);
CppUnit_addTest(pSuite, TimerTest, testCancelAllStop);
CppUnit_addTest(pSuite, TimerTest, testCancelAllWaitStop);
CppUnit_addTest(pSuite, TimerTest, testFunc);
return pSuite;
}

View File

@@ -35,6 +35,7 @@ public:
void testCancel();
void testCancelAllStop();
void testCancelAllWaitStop();
void testFunc();
void setUp();
void tearDown();