EventBaseThread::EventBaseThread() : EventBaseThread(true) {}
-EventBaseThread::EventBaseThread(bool autostart, EventBaseManager* ebm)
+EventBaseThread::EventBaseThread(
+ bool autostart,
+ EventBaseManager* ebm,
+ folly::StringPiece threadName)
: ebm_(ebm) {
if (autostart) {
- start();
+ start(threadName);
}
}
return !!th_;
}
-void EventBaseThread::start() {
+void EventBaseThread::start(folly::StringPiece threadName) {
if (th_) {
return;
}
- th_ = std::make_unique<ScopedEventBaseThread>(ebm_);
+ th_ = std::make_unique<ScopedEventBaseThread>(ebm_, threadName);
}
void EventBaseThread::stop() {
#pragma once
+#include <folly/Range.h>
#include <memory>
namespace folly {
class EventBaseThread {
public:
EventBaseThread();
- explicit EventBaseThread(bool autostart, EventBaseManager* ebm = nullptr);
+ explicit EventBaseThread(
+ bool autostart,
+ EventBaseManager* ebm = nullptr,
+ folly::StringPiece threadName = folly::StringPiece());
explicit EventBaseThread(EventBaseManager* ebm);
~EventBaseThread();
EventBase* getEventBase() const;
bool running() const;
- void start();
+ void start(folly::StringPiece threadName = folly::StringPiece());
void stop();
private:
#include <chrono>
#include <folly/Baton.h>
+#include <folly/ThreadName.h>
#include <folly/io/async/EventBaseManager.h>
#include <folly/portability/GTest.h>
class EventBaseThreadTest : public testing::Test {};
TEST_F(EventBaseThreadTest, example) {
- EventBaseThread ebt;
+ EventBaseThread ebt(true, nullptr, "monkey");
Baton<> done;
- ebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
+ ebt.getEventBase()->runInEventBaseThread([&] {
+ EXPECT_EQ(getCurrentThreadName().value(), "monkey");
+ done.post();
+ });
ASSERT_TRUE(done.timed_wait(seconds(1)));
}