gpt4 book ai didi

c - 避免在这种情况下忙等待

转载 作者:行者123 更新时间:2023-11-30 17:47:16 25 4
gpt4 key购买 nike

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

其中一个线程必须以非阻塞方式发送推送通知(因此我将使用 libcurl,因为它支持多接口(interface)和 SSL)。主线程必须将作业传递给工作线程,我考虑使用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。

最佳答案

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

关于c - 避免在这种情况下忙等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18981057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com