【问题标题】:Avoiding busy waiting in this situation避免在这种情况下忙于等待
【发布时间】:2013-09-29 15:16:39
【问题描述】:

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

这样的线程必须以非阻塞方式发送推送通知(因此,由于多接口和 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。

【问题讨论】:

    标签: c multithreading libcurl apr


    【解决方案1】:

    很难准确回答您的问题,只是没有足够的信息。但是,如果您显示的代码是工作代码,即在线程中启动,并且经常需要推送通知,那么我看不出这不起作用的原因。至于你的问题这是一种忙碌的等待,有没有更好的解决方案?我通常会在它负责做的任务完成后做一些事情导致线程空闲,例如使用睡眠(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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-14
      • 2013-04-17
      • 1970-01-01
      • 1970-01-01
      • 2020-07-21
      • 1970-01-01
      相关资源
      最近更新 更多