7

1つで約1000個の記述子を処理する必要がありpoll(Linux固有であるため使用できませんepoll)、それらを動的に追加/削除できる必要があります(新しい接続を処理して閉じたものを削除します)。

これは、反復ごとに記述子配列を再結合する必要があることを意味します。

これは技術的な観点からはかなり明白ですが、誰かがそれを行うための美しい方法を知っていますか?

4

1 に答える 1

5

死んだ記述子を配列に保持し、時々パージします。簡単に削除できるように、各記述子の場所も維持しますが、これはさらに最適化できます。トリックは、配列を毎回再配置するのではなく、無効な記述子を配列に保持することです。

例えば:

struct pollfd pfds[MY_MAX_FDS];
int nfds = 0;
enum { INVALID_FD = -1 };
....

typedef std::map<int,int> desc_to_index_t;
desc_to_index_t d2i;
....
// add descriptor
if (nfds == MY_MAX_FDS){
    // purge old fds
    // go through pfds and remove everything that has invalid fd
    // pfds should point to a condensed array of valid objects and nfds should be updated as well, as long as d2i.
}
pfds[nfds] = { desc, events, revents};
d2i.insert(std::make_pair(desc,nfds));
++nfds;
....
// remove descriptor
desc_to_index_t::iterator it = d2i.find(desc);
assert(it != d2i.end());
pfds[it->second] = { INVALID_FD, 0, 0 };
d2i.erase(it);

この方法では、特定のしきい値を超えた場合にのみパージする必要があり、毎回アレイを構築する必要はありません。

于 2012-11-06T12:22:39.183 に答える