From 013b1dc344ffd3d6d8287fe3a8b09cd55c38031c Mon Sep 17 00:00:00 2001 From: Vladislav Khorev Date: Mon, 4 Mar 2013 19:09:56 +0000 Subject: [PATCH] fixing bugs --- include/Utils/ThreadUtilsImpl.h | 31 ++++++++++++++++++++++++------- src/Utils/ThreadUtils.cpp | 8 +++++++- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/include/Utils/ThreadUtilsImpl.h b/include/Utils/ThreadUtilsImpl.h index f2944a6..81cc3f9 100644 --- a/include/Utils/ThreadUtilsImpl.h +++ b/include/Utils/ThreadUtilsImpl.h @@ -6,6 +6,14 @@ namespace SE { + + namespace ST + { + extern boost::condition_variable FunctionFinishedCondition; + + extern boost::mutex FunctionMutex; + } + template RETURNTYPE PerformInMainThread(boost::function f) @@ -18,21 +26,30 @@ namespace SE { RETURNTYPE result; - boost::mutex serviceLock; - + bool functionCalled = false; + boost::function func = - [&result, &f, &serviceLock] () + [&] () { result = f(); - serviceLock.unlock(); + + { + boost::mutex::scoped_lock lock(FunctionMutex); + functionCalled = true; + } + FunctionFinishedCondition.notify_one(); }; - serviceLock.lock(); + ST::MainThreadIoService.post(func); - serviceLock.lock(); - serviceLock.unlock(); + boost::mutex::scoped_lock lock(FunctionMutex); + + while (!functionCalled) + { + FunctionFinishedCondition.wait(lock); + } return result; diff --git a/src/Utils/ThreadUtils.cpp b/src/Utils/ThreadUtils.cpp index 30a303b..eec7120 100644 --- a/src/Utils/ThreadUtils.cpp +++ b/src/Utils/ThreadUtils.cpp @@ -2,13 +2,19 @@ namespace SE { - + namespace ST { boost::asio::io_service MainThreadIoService; boost::thread::id MainThreadId; + + boost::condition_variable FunctionFinishedCondition; + + boost::mutex FunctionMutex; } + + #ifndef UTILS_ENGINE void AssertIfInMainThread() {