std::thread,std::move,std::function
std::thread thread_ = std::make_shared<std::thread>(&EventLoopThread::loop_thread, this, pre, post);
private:
void loop_thread(const Functor& pre, const Functor& post) {
hlogi("EventLoopThread started, tid=%ld", hv_gettid());
setStatus(kStarted);if (pre) {
loop_->queueInLoop([this, pre]{//看Functor定义也可以写成 loop_->queueInLoop([this, pre](){
if (pre() != 0) {
loop_->stop();
}
});
}
void queueInLoop(Functor fn) {
postEvent([fn](Event* ev) {//参见EventCallback定义
if (fn) fn();
});
}
typedef std::function<void()> Functor;
void postEvent(EventCallback cb) {
if (loop_ == NULL) return;EventPtr ev = std::make_shared<Event>(cb);//cb回调传递给Event对象
typedef std::function<void(Event*)> EventCallback;
struct Event {
hevent_t event;
EventCallback cb;Event(EventCallback cb = NULL) {
memset(&event, 0, sizeof(hevent_t));
this->cb = std::move(cb);
}
};
目的就是既解决了传参又不用写一个个单独的回调函数了