`select`从多个线程同时FD [英] `select` on same fd from multiple threads

查看:388
本文介绍了`select`从多个线程同时FD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我叫会发生什么选择从多个线程相同的开放文件描述符?

What will happen if I call select on the same open file descriptor from multiple threads?

这是记载地方?

推荐答案

按照 POSIX 2008 选择 规范中,没有什么是禁止从两个线程都调用选择在同一时间。

这是合理的推断,如果两个线程监视重叠组文件描述符和一些常见的文件描述符成为读写或已经诊断错误,那么这两个线程可以结束了一个报告,共同文件描述符准备。这不能保证;有定时问题不用担心,它可能取决于线程的调度等,还意味着一个线程可能最终没有找到数据,以文件描述符有人告诉包含的数据读取读取,precisely因为其他线程最先到达那里。数据的任何给定字节将由线程只有一个被读出。

It is reasonable to infer that if both threads are monitoring overlapping sets of file descriptors and some of the common file descriptors become readable or writable or have errors diagnosed, then both threads may end up with a report that the common file descriptors are ready. This cannot be guaranteed; there are timing issues to worry about, and it may depend on scheduling of the threads, etc. It also means one of the threads may end up not finding data to read on a file descriptor that it was told contained data to read, precisely because the other thread got there first. Any given byte of data will be read by just one of the threads.

这篇关于`select`从多个线程同时FD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆