Skip to content

Instantly share code, notes, and snippets.

@shlevy
Created February 14, 2018 16:36
Show Gist options
  • Save shlevy/962dd2d20deb3566bcea979093cd64b7 to your computer and use it in GitHub Desktop.
Save shlevy/962dd2d20deb3566bcea979093cd64b7 to your computer and use it in GitHub Desktop.
#include <thread>
#include <mutex>
#include <iostream>
extern "C" {
#include <poll.h>
#include <unistd.h>
}
std::mutex stdio_mutex;
static void monitor_thread(int fd) {
struct pollfd fds[1];
fds[0].fd = fd;
fds[0].events = 0;
poll(fds, 1, -1);
{
std::lock_guard<std::mutex> guard(stdio_mutex);
std::cerr << "Poll returned: " << fds[0].revents << '\n';
}
}
int main(int argc, char ** argv) {
int pipe_fds[2];
if (pipe(pipe_fds) == -1)
return 1;
std::thread t(monitor_thread, pipe_fds[0]);
{
std::lock_guard<std::mutex> guard(stdio_mutex);
std::cerr << "Thread forked\n";
}
close(pipe_fds[1]);
sleep(10);
{
std::lock_guard<std::mutex> guard(stdio_mutex);
std::cerr << "FD closed\n";
}
t.join();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment