Set names in NamedThreadFactory in the new thread
authorChristopher Dykes <cdykes@fb.com>
Tue, 19 Sep 2017 02:02:08 +0000 (19:02 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Tue, 19 Sep 2017 02:05:29 +0000 (19:05 -0700)
Summary: This makes it now both work, and compile, on Windows.

Reviewed By: yfeldblum

Differential Revision: D5811100

fbshipit-source-id: 5d6bfc04ed8e60417615da15bd197769e0c79c11

folly/executors/NamedThreadFactory.h

index bcd4a6bbe9289e0766f8cbd33bb3b13a3c6e50cf..856a529557c261225a498843c86fecb195e810b3 100644 (file)
@@ -33,10 +33,12 @@ class NamedThreadFactory : public ThreadFactory {
       : prefix_(prefix.str()), suffix_(0) {}
 
   std::thread newThread(Func&& func) override {
-    auto thread = std::thread(std::move(func));
-    folly::setThreadName(
-        thread.native_handle(), folly::to<std::string>(prefix_, suffix_++));
-    return thread;
+    auto name = folly::to<std::string>(prefix_, suffix_++);
+    return std::thread(
+        [ func = std::move(func), name = std::move(name) ]() mutable {
+          folly::setThreadName(name);
+          func();
+        });
   }
 
   void setNamePrefix(folly::StringPiece prefix) {