中断从另一个线程调用zmqpp :: poller :: poll()

时间:2015-08-20 21:32:47

标签: c++ zeromq publish-subscribe

我正在使用zmqppZeroMQ C ++绑定来构建PUB / SUB通信。

在订阅方,有2个主题 - mainThreadpollingThread

pollingThread包含对zmqpp::poller::poll(zmqpp::poller::wait_forever)的阻止调用。

如何,从mainThread开始,我可以中断/取消pollingThreadpoll()的来电吗?

EDIT1:我正在使用std::thread,所以I can't interrupt the thread

EDIT2:这基本上是pollingThread的功能。

void pollingThread()
{
    while (threadCanRun())
    {
        // wait indefinitely for a new message
        if (mySocketPoller->poll())
        {
            functionToCallWhenAMessageArrives();
        }
    }
}

1 个答案:

答案 0 :(得分:2)

IMO建议的方法是依靠消息传递来处理这个问题。

这意味着:

  1. 创建一对INPROC PAIR插槽。每个线程一个。连接它们。
  2. 轮询线程上的
  3. ,也是PAIR套接字上的poll()
  4. 当您想要中断轮询线程时,请从PAIR套接字向轮询线程的PAIR套接字发送一条消息。
  5. 然后会回复poll()
  6. 请注意,建议使用此解决方案时,需要更改代码位。

        if (mySocketPoller->poll())
        {
            if (mySocketPoller->has_input(my_pair_socket))
            {
             // interrupted.
            }
            functionToCallWhenAMessageArrives();
        }
    
相关问题