-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
另一种线程通信方式——eventfd #13
Comments
认识
|
实现创建一个
|
使用在说明 对多个生产者而言,它们会一直写数据到公共的队列中,之后唤醒消费者(如果消费者处于睡眠状态的话): func producer(data){
// 投递消息
write_data_to_queue(quene, data)
// 唤醒消费者去处理(让 eventfd 递增 1)
write(eventfd, 1, 8)
} 对单个消费者而言,是另一个线程,它会将这个 func Consumer(){
// 添加 eventfd 到监听池,并设置标志为读
epoll_ctl(ep, EPOLL_CTL_ADD, eventfd, xxx);
// 进入轮询
for {
// 等待唤醒
epoll_wait(ep, ... );
// 读取新添加到列表里的元素个数,并且进行处理;
n = read(eventfd, ... )
// 根据n 处理全局队列中的数据
..
}
} |
eventfd
是一种linux上的线程通信方式,和信号量等其他线程通信不同的是,eventfd
可以用于进程间的通信,还可以用于内核发信号给用户态的进程,eventfd
是linux上的系统调用,本质上是用于事件通知。这种文件fd
无法传输数据,只能用来传输事件,通常用于生产者消费者模式的事件实现。本 issue 旨在明确
eventfd
的用法,为说明原理会对对其实现细节作简要描述。The text was updated successfully, but these errors were encountered: