gpt4 book ai didi

c++ - Linux C++ : libaio callback function never called?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:08:01 24 4
gpt4 key购买 nike

我在 ubuntu 16.10 上使用 g++ 6.2,测试 libaio 功能:

1. I was trying to test io_set_callback() function
2. I was using main thread and a child thread to talk by a pipe
3. child thread writes periodically (by alarm timer, signal), and main thread reads

我希望使用“回调”功能来接收通知。它没有按预期工作:从未调用回调函数“read_done”

我的问题:

1. I expected my program should call "read_done" function, but actually not.
2. Why the output prints 2 "Enter while" each time?
I hope it only print together with "thread write msg:..."
3. I tried to comment out "io_getevents" line, same result.

我不确定回调模式是否还需要io_getevents?那么如何修复我的程序以使其按预期工作呢?谢谢。

最佳答案

您需要将io_queue_run(3)io_queue_init(3) 集成到您的程序中。尽管这些不是新功能,但它们似乎并未出现在大量当前发布的发行版的联机帮助页中。这里有几个联机帮助页:

http://manpages.ubuntu.com/manpages/precise/en/man3/io_queue_run.3.html http://manpages.ubuntu.com/manpages/precise/en/man3/io_queue_init.3.html

当然,联机帮助页实际上并没有说明,但 io_queue_run 会调用您在 io_set_callback 中设置的回调。

更新:嗯。这是 Centos/RHEL 上 libaio-0.3.109 的 io_queue_run 的源代码(LGPL 许可证,版权所有 2002 Red Hat, Inc.)

int io_queue_run(io_context_t ctx)
{
static struct timespec timeout = { 0, 0 };
struct io_event event;
int ret;

/* FIXME: batch requests? */
while (1 == (ret = io_getevents(ctx, 0, 1, &event, &timeout))) {
io_callback_t cb = (io_callback_t)event.data;
struct iocb *iocb = event.obj;

cb(ctx, iocb, event.res, event.res2);
}

return ret;
}

如果没有 io_queue_wait 调用,您永远不会想真正调用它。并且,io_queue_wait 调用在 Centos/RHEL 6 和 7 附带的 header 中被注释掉了。我认为您不应该调用此函数。

相反,我认为您应该将此源合并到您自己的代码中,然后修改它以执行您想要的操作。您可以非常简单地向此 io_queue_run 添加一个超时参数,并用它替换您对 io_getevents 的调用,而不是使用 io_queue_wait。这里甚至有一个补丁使 io_queue_run 变得更好:https://lwn.net/Articles/39285/ ).

关于c++ - Linux C++ : libaio callback function never called?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42643328/

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