简体   繁体   English

epoll_wait 在“epoll_event”中存储了一个不同的值

[英]epoll_wait stores a different value in “epoll_event”

I'll try keep this simple.我会尽量保持简单。 I have the code below:我有以下代码:

epoll_event event;

event.events = EPOLLIN | EPOLLET;
event.data.fd = clientSock; // this is equal to "7"
event.data.ptr = myPtr;
epoll_ctl(epoll, EPOLL_CTL_ADD, client, &event);


//Another thread

 epoll_wait(epoll, &event2, MAX_EVENTS, EPOLL_TIMEOUT);

 // This is the strange part... 
 cout << event2.data.fd; //output is different from "7"

But, if I dont add a ptr to event.data.ptr (which I did before I called epoll_wait ), the value of event2.data.fd is correct (7).但是,如果我不向event.data.ptr添加一个 ptr(我在调用epoll_wait之前就这样做了),那么event2.data.fd的值是正确的 (7)。 What's causing this?这是什么原因造成的?

The type of the data member of struct epoll_event is a union .struct epoll_eventdata成员的类型是union As such, only one of its members contains a value at any given time, so when you assign to event.data.ptr you replace the value previously written to event.data.fd .因此,在任何给定时间,只有一个成员包含一个值,因此当您分配给event.data.ptr时,您会替换之前写入event.data.fd的值。 The subsequent epoll_ctl call therefore probably does not express interest in the events you think it does, but in any case, you should expect to read back only the ptr member from the resulting event data, not the fd member.因此,随后的epoll_ctl调用可能不会对您认为的事件表示兴趣,但无论如何,您应该期望从结果事件数据中只读回ptr成员,而不是fd成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM