*/
#include "folly/experimental/ThreadedRepeatingFunctionRunner.h"
+#include <folly/ThreadName.h>
#include <glog/logging.h>
#include <iostream>
}
void ThreadedRepeatingFunctionRunner::add(
+ std::string name,
RepeatingFn fn,
std::chrono::milliseconds initialSleep) {
- threads_.emplace_back(
- &ThreadedRepeatingFunctionRunner::executeInLoop,
- this,
- std::move(fn),
- initialSleep);
+ threads_.emplace_back([
+ name = std::move(name),
+ fn = std::move(fn),
+ initialSleep,
+ this
+ ]() mutable {
+ setThreadName(name);
+ executeInLoop(std::move(fn), initialSleep);
+ });
}
bool ThreadedRepeatingFunctionRunner::waitFor(
/**
* Run your noexcept function `f` in a background loop, sleeping between
* calls for a duration returned by `f`. Optionally waits for
- * `initialSleep` before calling `f` for the first time.
+ * `initialSleep` before calling `f` for the first time. Names the thread
+ * using up to the first 15 chars of `name`.
*
* DANGER: If a non-final class has a ThreadedRepeatingFunctionRunner
* member (which, by the way, must be declared last in the class), then
* your threads.
*/
void add(
+ std::string name,
RepeatingFn f,
std::chrono::milliseconds initialSleep = std::chrono::milliseconds(0));
}
void start() {
- runner_.add([this]() {
+ runner_.add("Foo", [this]() {
++data;
return std::chrono::seconds(0);
});
}
void start() {
- runner_.add([this]() {
+ runner_.add("FooLongSleep", [this]() {
data.store(1);
return 1000h; // Test would time out if we waited
});