From d0b44af050c993772e8ef8bafb4b4fa0c41c2978 Mon Sep 17 00:00:00 2001 From: Anand Thakker Date: Thu, 14 Jun 2018 10:37:07 -0400 Subject: Move target Object constructor args into thread --- include/mbgl/util/thread.hpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/include/mbgl/util/thread.hpp b/include/mbgl/util/thread.hpp index 8230d8778f..b5f79aa28d 100644 --- a/include/mbgl/util/thread.hpp +++ b/include/mbgl/util/thread.hpp @@ -58,16 +58,27 @@ public: // up and running. std::shared_ptr mailbox_ = std::make_shared(noopScheduler); mailbox = mailbox_; - - thread = std::thread([&, sharedMailbox = std::move(mailbox_), runningPromise = std::move(running_)] { + + auto tuple = std::make_tuple(std::forward(args)...); + + thread = std::thread([ + this, + name, + tuple, + sharedMailbox = std::move(mailbox_), + runningPromise = std::move(running_) + ] { platform::setCurrentThreadName(name); platform::makeThreadLowPriority(); util::RunLoop loop_(util::RunLoop::Type::New); loop = &loop_; - Actor* actor = new (&actorStorage) Actor(std::move(sharedMailbox), std::forward(args)...); - + Actor* actor = emplaceActor( + std::move(sharedMailbox), + std::move(tuple), + std::make_index_sequence::value>{}); + // Replace the NoopScheduler on the mailbox with the RunLoop to // begin actually processing messages. actor->mailbox->setScheduler(this); @@ -161,6 +172,11 @@ private: assert(loop); loop->schedule(mailbox_); } + + template + Actor* emplaceActor(std::shared_ptr sharedMailbox, ArgsTuple args, std::index_sequence) { + return new (&actorStorage) Actor(std::move(sharedMailbox), std::move(std::get(std::forward(args)))...); + } NoopScheduler noopScheduler; std::weak_ptr mailbox; -- cgit v1.2.1