- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个多线程 epoll 服务器。我创建了一个 epoll fd,然后我将让 X 个线程休眠,等待 epoll_wait()
来自 SAME epoll fd 的任何事件。
现在我的问题是:如何唤醒 N 个线程,其中 N > 1 && N < X?
直到现在,我一直在使用 Linux 特定的 eventfd 工具,它只在 1 个线程上工作得很好,但现在有多个线程在等待 SAME epoll fd,出现了一个问题:
情况 1) LT:如果我将我的 eventfd 添加为“级别触发”模式,所有 线程将在我写入 eventfd 时唤醒,这就是级别触发模式的工作原理:一次fd 改变状态让我们唤醒所有线程。
N = X
情况 2) ET:如果我将我的 eventfd 添加为“边缘触发”模式,当我写入 eventfd 时,只有 1 线程将被唤醒,这就是边缘触发模式的工作原理: 在我从 read(eventfd, ...);
收到 EAGAIN
之前,不再有 epollfd 事件。
N = 1
案例 3)我也尝试过使用自管道技巧,向管道写入 N 次会唤醒 N 个线程。相反,它不会工作:它不可靠,有时一个线程从管道读取 2 个“ token ”,有时是 1 个或 3 个。
N = 随机
在我尝试过的所有情况下,我不能只得到 N=N,我不能只唤醒 N 个线程,而是 1 或 ALL,或 RANDOM。我错过了什么?有什么想法吗?注意:我还尝试了特定于 eventfd 的 EFD_SEMAPHORE
标志,没有任何帮助。
最佳答案
根据 eventfd手册页。
The file descriptor is readable (the select(2) readfds argument; the poll(2) POLLIN flag) if the counter has a value greater than 0.
通过创建带有 EFD_SEMAPHORE
标志的 eventfd:
(if) 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.
使用信号量(EFD_SEMAPHORE
标志),NONBLOCK(EFD_NONBLOCK
标志)eventfd 并等待级别触发的 epoll()
,或普通的 poll()
。
使用 eventfd_write(fd, N)
编写 N 个线程你想醒来。
当线程唤醒时,您将执行 read()
。如果你得到 EAGAIN
错误,你可以回去 sleep 因为 N 次成功的读取已经完成并且因此 N 个线程知道它们必须保持清醒。
所有线程唤醒(thundering herd 问题)。
关于linux - 多线程 epoll 服务器 : wake up N threads sleeping on the same epoll fd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28540910/
正如我不断发现的那样,有各种各样的文件描述符——几乎所有的东西都是围绕文件描述符抽象出来的:常规文件、套接字、信号和计时器(例如)。所有文件描述符都只是整数。 给定一个文件描述符,是否可以知道它是什么
socket fd 长什么样子? 什么是 socket fd ?粗糙的来讲,就是网络 fd,比如我们最常见的 C/S 客户端服务端的编程模式,就是网络通信的一种方式。撇开底层
如果我调用了shutdown(fd,SHUT_RDWR),但没有调用close(fd),会发生什么? inline void CSocket::close() { if (_socket_fd
我有以下结构: struct myfds_for_nic { int fd1; int fd2; int fd3; int fd4; int fd5;
fd 是否有等价于 fseek 的东西?我已经使用 int fds 很长时间了,想使用 fseek...但我知道没有搜索功能。 提前致谢! 最佳答案 参见 POSIX 函数 lseek(2) : SY
我正在使用 Clojure 的 core.logic CLP(FD) 库(core.logic 版本 0.8.3)开发一种简单的方形打包算法。 正方形的表示方式如下: [[[x11 y11] [x12
我正在学习 linux 操作系统,我有一个关于管道的问题。 我想实现一个管道。 所以我定义了int fd[2]; 但是为什么fd[0]是读而fd[1]是写呢? 0代表stdin吗? (我认为是写)而1
我知道进程的pid,需要获取它使用的socketfd,所以在/proc/$pid/中查找fd,例如: $ ls -la /proc/1442/fd | grep socket lrwx------ 1
我正在检查当前 fatrace 的源代码。 调用fanotify获取数据值的主循环如下: res = read (fan_fd, buffer, 4096); ... data
Docker daemon documentation建议大多数设置使用以下 hosts 选项: dockerd -H fd:// 我猜 fd 代表文件描述符。我不明白 fd 如何用于套接字通信。 我
在执行之前:os.read(fd,1024) 我想检查是否会有输出,而不是挂起直到收到输出。由于 fd 是一个 int 对象,我不能这样做: os.fstat(f.fileno()).st_size
我有一个程序 ( https://github.com/raboof/connbeat ) 依赖于 /proc/[pid]/fd/* 来查找给定(网络)inode 的进程。 /proc/[pid]/f
#define STACK_SIZE (1024 * 1024) static char container_stack[STACK_SIZE]; char* const container_args
inotify file in C 我看过下面的代码用来调用 (void) inotify_rm_watch(fd, wd); (void) close(fd); 为什么不呢? inotify_rm_
下面的小 C 程序(我们称之为 pointless): /* pointless.c */ #include #include void main(){ write(STDOUT_FILENO
考虑这个代码示例: #include #include #include int main() { //this file exists and contains data: "ABCD
在父进程中close(fd[1]);, 为什么它会跳过第一个 fd[1](替换为父 STD_OUT)并在子进程中关闭 fd[1]? #define STD_INPUT 0 #define STD_OU
需要使用代理设置运行模拟器,我在命令提示符下使用以下命令来启动模拟器 emulator -avd AVD_for_3_7_WVGA_Nexus_One -http-proxy http://usern
我正在 appcelerator studio 中创建应用程序。在我向其添加 admob 模块之前,它在我的 Android 6 Lenovo a7000 上正常运行。现在我收到这些错误: [ERRO
我不明白必须如何解决以下问题。非常感谢任何帮助学习如何解决这个问题的人! Consider Relation Schema R = {ABCDEFG} with a set of Functional
我是一名优秀的程序员,十分优秀!