// cpp#include <iostream>#include <sys/timerfd.h>#include <poll.h>#include <unistd.h>#include <assert.h>using namespace std;int main() {struct itimerspec timebuf;int timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);timebuf.it_interval = {1, 0}; // period timeout value = 1stimebuf.it_value = {5, 100}; // initial timeout value = 5s100nstimerfd_settime(timerfd, 0, &timebuf, NULL);struct pollfd fds[1];int len = sizeof(fds) / sizeof(fds[0]);fds[0].fd = timerfd;fds[0].events = POLLIN | POLLERR | POLLHUP;while (true){int n = poll(fds, len, -1);for (int i = 0; i < len && n-- > 0; ++i) {if (fds[i].revents & POLLIN){uint64_t val;int ret = read(timerfd, &val, sizeof(val));if (ret != sizeof(val)){cerr << "read " << ret << "bytes instead of 8 frome timerfd" << endl;break;}cout << "timerfd = " << timerfd << " timeout!" << endl;}}}close(timerfd);return 0;}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。