From ff1152e55dbfc9ddc9774386767406f6fdcc686a Mon Sep 17 00:00:00 2001 From: drltc Date: Tue, 9 Dec 2014 16:02:32 -0500 Subject: [PATCH] Add sync_call() for calling a functor in another thread and waiting on it --- include/fc/thread/thread.hpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/include/fc/thread/thread.hpp b/include/fc/thread/thread.hpp index 040d877..bb82710 100644 --- a/include/fc/thread/thread.hpp +++ b/include/fc/thread/thread.hpp @@ -206,6 +206,22 @@ namespace fc { return fc::thread::current().schedule( fc::forward(f), t, desc, prio ); } + /** + * Call f() in thread t and block the current thread until it returns. + * + * If t is null, simply execute f in the current thread. + */ + template + auto sync_call( thread* t, Functor&& f, const char* desc FC_TASK_NAME_DEFAULT_ARG, priority prio = priority()) -> decltype(f()) + { + if( t == nullptr ) + return f(); + + typedef decltype(f()) Result; + future r = t->async( f, desc, prio ); + return r.wait(); + } + } // end namespace fc #ifdef _MSC_VER