C++11 lambda块参数传递

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);
    }
};

 目的就是既解决了传参又不用写一个个单独的回调函数了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值