用于存储事件多路复用器数据的线程安全数据结构(epoll等)

时间:2017-10-12 11:05:44

标签: c++ multithreading epoll

我正在尝试创建一个高效的多线程事件多路复用器(在linux上使用epoll,在windows上使用iocp)

对于我添加到epoll的每个事件源,我需要存储一些其他信息。

起初,我已经使用了一个动态增长的阵列(因为缓存一致性),但事实证明,这不是一个好的解决方案,因为我需要保持锁定的保护互斥锁长。

// this function is OK, performance wise
void Multiplexer::add(int fd) {
    mutex.lock();
    <add fd int my array>
    mutex.unlock();
    <add fd into epoll, setting epoll_data.u32 to fd's index in the array>
}

void Multiplexer::poll() {
    events = <call_epoll>
    mutex.lock();  // here, I need to keep mutex locked for a long time, as array can grow from a different thread which calls add
    for (events) { // or, the other solution could be that I lock/unlock mutex each time in the for loop, which is bad for performance
         <handle_event_using_array>
    }
    mutex.unlock();
}

我的第二个想法是使用list,但这样我可能会失去缓存一致性的良好属性(它可能不那么重要,所以我可能会这样做)。

这个问题有更好的数据结构吗?或者更好的想法如何解决这个问题?

0 个答案:

没有答案
相关问题