From 753b6c5674d3d5eb466fca8d7f28dddff31a2ab7 Mon Sep 17 00:00:00 2001 From: Jake Farrell Date: Tue, 6 Dec 2011 01:41:59 +0000 Subject: [PATCH] Thrift-1444:FunctionRunner - add syntactic sugar to create shared_ptrs Client: cpp Patch: Dave Watson Simplify FunctionRunner addTask calls. git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1210741 13f79535-47bb-0310-9956-ffa450edef68 --- lib/cpp/src/concurrency/FunctionRunner.h | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/lib/cpp/src/concurrency/FunctionRunner.h b/lib/cpp/src/concurrency/FunctionRunner.h index 5a8cd3dc..f2162d68 100644 --- a/lib/cpp/src/concurrency/FunctionRunner.h +++ b/lib/cpp/src/concurrency/FunctionRunner.h @@ -34,16 +34,15 @@ namespace apache { namespace thrift { namespace concurrency { * void* my_thread_main(void* arg); * shared_ptr factory = ...; * // To create a thread that executes my_thread_main once: - * shared_ptr thread = - * factory->newThread(shared_ptr( - * new FunctionRunner(my_thread_main, some_argument))); + * shared_ptr thread = factory->newThread( + * FunctionRunner::create(my_thread_main, some_argument)); * thread->start(); * * bool A::foo(); * A* a = new A(); * // To create a thread that executes a.foo() every 100 milliseconds: - * factory->newThread(shared_ptr( - * new FunctionRunner(std::tr1::bind(&A::foo, a), 100)))->start(); + * factory->newThread(FunctionRunner::create( + * std::tr1::bind(&A::foo, a), 100))->start(); * */ @@ -56,6 +55,20 @@ class FunctionRunner : public Runnable { typedef std::tr1::function BoolFunc; + /** + * Syntactic sugar to make it easier to create new FunctionRunner + * objects wrapped in shared_ptr. + */ + static boost::shared_ptr create(const VoidFunc& cob) { + return boost::shared_ptr(new FunctionRunner(cob)); + } + + static boost::shared_ptr create(PthreadFuncPtr func, + void* arg) { + return boost::shared_ptr(new FunctionRunner(func, arg)); + } + + /** * Given a 'pthread_create' style callback, this FunctionRunner will * execute the given callback. Note that the 'void*' return value is ignored. -- 2.17.1