eventfd,是操作系统提供的事件通知机制,详情查看eventfd。
通过文件使用的形式,让俩个模块之间可以有交互,在一定程度上可以使用在进程间通信。
时间机制是一种典型的生产和消费的模式,对于eventfd,有俩种主要的使用方式。
read(2)
Each successful read(2) returns an 8-byte integer. A
read(2) fails with the error EINVAL if the size of the
supplied buffer is less than 8 bytes.
The semantics of read(2) depend on whether the eventfd
counter currently has a nonzero value and whether the
EFD_SEMAPHORE flag was specified when creating the eventfd
file descriptor:
1、If EFD_SEMAPHORE was not specified and the eventfd
counter has a nonzero value, then a read(2) returns 8
bytes containing that value, and the counter’s value is
reset to zero.
2、If EFD_SEMAPHORE was specified and the eventfd counter
has a nonzero value, then a read(2) returns 8 bytes
containing the value 1, and the counter’s value is
decremented by 1.
3、If the eventfd counter is zero at the time of the call
to read(2), then the call either blocks until the
counter becomes nonzero (at which time, the read(2)
proceeds as described above) or fails with the error
EAGAIN if the file descriptor has been made
nonblocking.
可以简单模拟下俩种的差别,代码还是很简单的。
#include <sys/eventfd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
int _event_fd;
int openEventFd() {
_event_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC | EFD_SEMAPHORE);
}
int testWriteEventFd() {
uint64_t val = 1;
size_t n = write(_event_fd, &val, sizeof(val));
if (n != sizeof(n)) {
cerr << "write event_fd failed!!!" << endl;
return -1;
}
return 0;
}
int testReadEventFd() {
uint64_t val = 1;
size_t n = read(_event_fd, &val, sizeof(val));
if (errno == EAGAIN) {
return 0;
}
if (n != sizeof(n)) {
cerr << "read event_fd failed!!!" << endl;
return -1;
}
cout << "read " << val << endl;
return 0;
}