engine/include/Utils/ThreadUtilsImpl.h

44 lines
731 B
C
Raw Normal View History

2013-02-03 13:11:16 +00:00
#ifndef THREAD_UTILS_IMPL_H_INCLUDED
#define THREAD_UTILS_IMPL_H_INCLUDED
#include "include/Engine.h"
namespace SE
{
template<typename RETURNTYPE>
RETURNTYPE PerformInMainThread(boost::function<RETURNTYPE()> f)
{
2013-02-21 13:14:46 +00:00
if (boost::this_thread::get_id() == ST::MainThreadId)
2013-02-03 13:11:16 +00:00
{
return f();
}
else
{
RETURNTYPE result;
2013-02-21 08:35:47 +00:00
boost::mutex serviceLock;
2013-02-03 13:11:16 +00:00
2013-02-21 08:35:47 +00:00
boost::function<void()> func =
[&result, &f, &serviceLock] ()
{
result = f();
serviceLock.unlock();
};
2013-02-03 13:11:16 +00:00
2013-02-21 08:35:47 +00:00
serviceLock.lock();
2013-02-03 13:11:16 +00:00
2013-02-21 13:14:46 +00:00
ST::MainThreadIoService.post(func);
2013-02-03 13:11:16 +00:00
2013-02-21 08:35:47 +00:00
serviceLock.lock();
serviceLock.unlock();
2013-02-03 13:11:16 +00:00
return result;
2013-02-21 08:35:47 +00:00
}
2013-02-03 13:11:16 +00:00
}
} //namespace SE
#endif