0

我有一个使用不同线程来制作不同类型作业的进程。

一个这样的线程必须以非阻塞方式发送推送通知(因此,由于多接口和 SSL 支持,我将使用 libcurl)。主线程必须将作业传递给工作人员,我考虑使用 apache apr 消息队列进行消息传递。因为在同一个线程中,我必须检查传入的消息和 curl 句柄的可用性,我认为我将使用这样的东西:

while (1)
{
    while (apr_queue_try_pop(queue, &msg) == APR_SUCCESS)
    {
        // do something with the message
    }

    // perform a select or poll in the curl multi handle
    // treat the handles that are available for reads/writes
}

在线程启动函数中。

这有点忙等待,有更好的解决方案吗?

使用 C99 和 Linux x86_64。

4

1 回答 1

0

很难准确地回答你的问题,只是没有足够的信息。但是,如果您显示的代码是工作代码,即在线程中启动,并且经常需要推送通知,那么我看不出这不起作用的原因。至于你的问题,这是一种忙碌的等待,有没有更好的解决方案?一旦它负责执行的任务完成后,我通常会做一些事情导致线程空闲,例如使用睡眠(1000)。线程时间片的剩余部分不用于循环。像这样:

while (1)
{
    while (apr_queue_try_pop(queue, &msg) == APR_SUCCESS)
    {
        // do something with the message
    }

    // perform a select or poll in the curl multi handle
    // treat the handles that are available for reads/writes
    Sleep(1000);
}
于 2013-09-24T15:01:41.427 回答