同时在 epoll 数据结构中使用 void *ptr 和 int fd
use of void *ptr and int fd in epoll data structure at same time
我正在使用epoll
系统调用来处理TCP会话中的多个客户端。
我已经在 fd 注册了epoll
:
epoll_ctl (efd, EPOLL_CTL_ADD, fd, &event);
并且能够与比较FD一起使用:
if(conn->getSockfd() == events[i].data.fd)
// ....
但作为 epoll 数据结构
typedef union epoll_data {
void *ptr;
int fd;
uint32_t u32;
uint64_t u64;
} epoll_data_t;
我想将一个对象(函数(映射到特定的fd
字段,这样我就可以在比较后调用这个函数。
但我没有得到正确的结果。可以做还是不做?如果是,那么我可以使用它吗?
您正在为epoll_data_t使用联合。因此,它一次只能存储其中一个字段。所以要么你的空白*,int,随便什么。
我注意到你用C++标记了你的问题,所以这是我来解决这个问题的:
您可以在类/结构中将描述符和函数绑定在一起,然后为您接受/创建的每个连接创建一个新实例。在构造过程中,您可以将 FD 和函数传递到新对象中。
这是一个快速且未经测试的概念:
template < typename FunctionType >
struct Connection
{
int FD;
std::function< FunctionType > DoStuff;
Connection( int descriptor, const std::function< FunctionType >& Func )
: FD ( descriptor )
, DoStuff ( Func )
}
然后你可以调用构造函数,将你的文件描述符和函数传递给它,用 std::bind 执行,然后通过执行 Connection.DoStuff(( 来调用它。
如果您使用占位符等,则可以使用参数等。有关 std::bind 和 std::function 的更多信息,请点击超链接。
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 在c++类上调用void函数
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 在派生函数中指定void*参数
- C++为什么尽管我调用了void函数,它却不起作用
- 如何从void函数输出字符串
- 我应该使用什么来代替void作为变体中的替代类型之一
- 奇怪的结构&GCC&clang(void*返回类型)
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 使用 void 指针:void **ptr.. 如何修改共享内存的值?
- 如何使用 epoll(void* event.data.ptr) 管理 Connection 的生命周期
- C++中的大小释放:全局运算符delete的正确行为是什么(void*ptr,std::size_t size)
- 我如何在void*和boost共享ptr之间切换
- 同时在 epoll 数据结构中使用 void *ptr 和 int fd
- OpenCL enqueueWriteImage no const void* ptr 在包装器中C++但在 C 函数中
- void(**vt)()=*(void(***)())ptr;c++中虚拟表的辅助变量
- 为什么 gcc 4.9.0 中没有定义"void operator delete(void* ptr, std::size_t size) noexcept;"?
- "(void)!ptr"做什么?
- Linux中的c++运行时错误::glibc检测到free():无效的下一个大小(快速):堆栈具有void ptr元素
- 表达"(ptr == 0) != (ptr == (void*)0)"真的是真的吗?