Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ posix管道作为消息队列:阻塞写入时会发生什么+;信号_C++_Linux_Pipe_Message Queue_Atomic - Fatal编程技术网

C++ posix管道作为消息队列:阻塞写入时会发生什么+;信号

C++ posix管道作为消息队列:阻塞写入时会发生什么+;信号,c++,linux,pipe,message-queue,atomic,C++,Linux,Pipe,Message Queue,Atomic,我想使用管道作为内部消息队列,如下所述: 根据glibc的文档,如果数据小于pipe_BUF,则对管道的写入是原子的。 但是:如果出现信号,写操作可能会中断。让我们假设,写入被阻塞,因为管道几乎满了。现在出现了一个信号。数据会自动写入管道吗 template <typename T> ssize_t put(const T& data) { static_assert(sizeof(T) < PIPE_BUF, "We use the atomi

我想使用管道作为内部消息队列,如下所述:

根据glibc的文档,如果数据小于pipe_BUF,则对管道的写入是原子的。

但是:如果出现信号,写操作可能会中断。让我们假设,
写入
被阻塞,因为管道几乎满了。现在出现了一个信号。数据会自动写入管道吗

template <typename T>
ssize_t put(const T& data)
{       
    static_assert(sizeof(T) < PIPE_BUF, "We use the atomic property of the pipe write. So sizeof(T) MUST be smaller than PIPE_BUF");

    int written;
    const size_t id = T::value;
    written = write(m_fds[write_fd], &id, sizeof(size_t));
    assert(written == sizeof(size_t));

    const size_t sT = sizeof(T);
    write(m_fds[write_fd], &sT, sizeof(size_t));
    assert(written == sizeof(size_t));

    write(m_fds[write_fd], &data, sizeof(T)); // * this blocks in example
    assert(written == sizeof(T));

    return sizeof(T);
}
模板
ssize\u t put(常数和数据)
{       
static_assert(sizeof(T)

信号中断略有不同。据英国《每日邮报》报道,管道是一种“慢速装置”。因此,除非设置了
sau RESTART
,否则
write
操作将以写入的数据数返回success。因此,最好始终检查write的返回值,以确保所有数据都已写入。或者,您可以屏蔽可能的信号。

谢谢!请问writev()的行为是否与此上下文中的write相同,以便将所有三项写入管道?否则我就需要一个互斥锁,原子属性对我来说毫无用处。“readv()和writev()执行的数据传输是原子的”