我修改了here找到的示例以使用两个io频道。在写入两个通道之前,似乎没有调用任何回调。之后,在写入fifos时会单独调用它们。我忘记了什么吗?
编辑: 正如下面Gormley所暗示的那样,解决方案是缺乏非阻塞。
read_fd1 = open("testfifo1", O_RDONLY | O_NONBLOCK);
...
read_fd2 = open("testfifo2", O_RDONLY | O_NONBLOCK);
对以下代码的更改使其立即响应。
代码:
#include <gtkmm/main.h>
#include <fcntl.h>
#include <iostream>
int read_fd1, read_fd2;
Glib::RefPtr<Glib::IOChannel> iochannel1, iochannel2;
// Usage: "echo "Hello" > testfifo<1|2>", quit with "Q"
bool MyCallback1(Glib::IOCondition io_condition)
{
Glib::ustring buf;
iochannel1->read_line(buf);
std::cout << "io 1: " << buf;
if (buf == "Q\n")
Gtk::Main::quit();
return true;
}
bool MyCallback2(Glib::IOCondition io_condition)
{
Glib::ustring buf;
iochannel2->read_line(buf);
std::cout << "io 2: " << buf;
if (buf == "Q\n")
Gtk::Main::quit();
return true;
}
int main(int argc, char *argv[])
{
// the usual Gtk::Main object
Gtk::Main app(argc, argv);
if (access("testfifo1", F_OK) == -1)
{
if (mkfifo("testfifo1", 0666) != 0)
return -1;
}
if (access("testfifo2", F_OK) == -1)
{
if (mkfifo("testfifo2", 0666) != 0)
return -1;
}
read_fd1 = open("testfifo1", O_RDONLY);
if (read_fd1 == -1)
return -1;
read_fd2 = open("testfifo2", O_RDONLY);
if (read_fd2 == -1)
return -1;
Glib::signal_io().connect(sigc::ptr_fun(MyCallback1), read_fd1, Glib::IO_IN);
Glib::signal_io().connect(sigc::ptr_fun(MyCallback2), read_fd2, Glib::IO_IN);
iochannel1 = Glib::IOChannel::create_from_fd(read_fd1);
iochannel2 = Glib::IOChannel::create_from_fd(read_fd2);
app.run();
if (unlink("testfifo1"))
std::cerr << "error removing fifo 1" << std::endl;
if (unlink("testfifo2"))
std::cerr << "error removing fifo 2" << std::endl;
return 0;
}
答案 0 :(得分:0)
这两个语句阻止程序进入主循环,直到两个fifos 写的是开放的。 fifos阻止,直到双方都连接
iochannel1 = Glib :: IOChannel :: create_from_fd(read_fd1); iochannel2 = Glib :: IOChannel :: create_from_fd(read_fd2);