There are tons of questions about non blocking pipes, but there are NO examples of code that can be copy&paste (with little correction) and used.
I got the idea
In my Linux multithreaded application (Ubuntu 16.04, x86_64, Native POSIX Threads, libc.so.6 version Ubuntu GLIBC 2.23-0ubuntu9) this code
int fd = fileno(pipe);
fcntl(fd, F_SETFL, O_NONBLOCK);
while (true)
        {
            ssize_t r = read(fd, buff, sizeof(buff));
            if ((r == -1) && (errno == EAGAIN)) // really need errno?
                continue;
            else
            {
                if (r > 0)
                    result += buff;
                else
                    break;
            }
        }
even with fctnl(...) causes fail as not only output of command but many another arbitrary values from RAM are often read. I think it is due to thread unsafety of the code.
(Example: execute echo 3 values, 10, 30, and 40, in parallel by 20 threads, read the values and display only the last value. Output: 40 QIh MIh 40 40 30...)
Another code from this answer works fine due to fgets implementation is thread-safe:
while (!feof(pipe))
{
    if (fgets(buff, sizeof(buff), pipe) != NULL)
    result += buff;
}