简体   繁体   English

AMQP-CPP,libev>从另一个线程停止ev_loop

[英]AMQP-CPP, libev > stop ev_loop from another thread

I use AMQP-CPP lib with libev backend. 我将AMQP-CPP lib与libev后端一起使用。 I try to create a class which will open a connection and do consuming. 我尝试创建一个类来打开连接并使用。 I want to run connection's loop in a worker thread in order not to block the main thread. 我想在工作线程中运行连接的循环,以免阻塞主线程。 That part of code looks like this 这部分代码看起来像这样

...
m_thread.reset(new std:thread([this]()
    {
        ev_run(m_loop, 0);
    }));
...

Then at some point I want to stop the loop. 然后在某个时候我想停止循环。 I have read that it is possible to do it with ev_break() function. 我读过,可以用ev_break()函数来做到这ev_break() However, it should be called from the same thread as ev_run() was called. 但是,应从与调用ev_run()相同的线程中调用它。 More search showed that ev_async_send() function might do that, but I cannot figure out how. 更多搜索显示ev_async_send()函数可以做到这一点,但我不知道如何做到。

How can I do it? 我该怎么做? Any ideas? 有任何想法吗?

Here is an example: 这是一个例子:

void asyncCallback(EV_P_ ev_async*, int)
{
    ev_break(m_loop, EVBREAK_ONE);
}

void MyClass::stopLoop()
{
    ev_async_init(&m_asyncWatcher, asyncCallback);
    ev_async_start(m_loop, &m_asyncWatcher);
    ev_async_send(m_loop, &m_asyncWatcher);

    m_thread->join();
}

// in the class async watcher should be defined
ev_async m_asyncWatcher;

By calling stopLoop() function from another thread it stops the loop that started from m_thread worker thread. 通过从另一个线程调用stopLoop()函数,它将停止从m_thread工作线程开始的循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM