STD ::列表和垃圾收集算法
std::list and garbage Collection algorithm
我有一台服务器,该服务器应要求将2个玩家放在一起,并在新线程中启动游戏Game
。
struct GInfo {Game* game; std::thread* g_thread};
while (true) {
players_pair = matchPlayers();
Game* game = new Game(players_pair);
std::thread* game_T = new std::thread(&Game::start, game);
GInfo ginfo = {game, game_T}
_actives.push_back(ginfo); // std::list
}
我正在写一个在另一个线程中运行的"垃圾收集器",以清洁终止游戏的内存。
void garbageCollector() {
while (true) {
for (std::list<Ginfo>::iterator it = _actives.begin(); it != _actives.end(); ++it) {
if (! it->game->isActive()) {
delete it->game; it->game = nullptr;
it->g_thread->join();
delete it->g_thread; it->g_thread = nullptr;
_actives.erase(it);
}
}
sleep(2);
}
}
这会产生一个segfault,我怀疑这是因为_active.erase(it)
在迭代循环中。为了进行故障排除,我将_actives
制作为std::vector
(而不是std::list
),并应用了相同的算法,但是使用索引而不是迭代器,它可以正常工作。
有办法解决吗?
算法,数据结构使用良好吗?有更好的方法来制作垃圾收集?
帮助您表示感谢!
如果您查看了擦除方法的文档,则将迭代器返回到删除的文档。
使用的方法是将返回的值分配给您的迭代器。
for (std::list<Ginfo>::iterator it = _actives.begin(); it != _actives.end();) {
if (! it->game->isActive()) {
delete it->game; it->game = nullptr;
it->g_thread->join();
delete it->g_thread; it->g_thread = nullptr;
it = _actives.erase(it);
}
else {
++it;
}
}
由于从erase
拾取返回值将迭代器推进到下一个元素,因此我们必须确保在发生这种情况时不要递增迭代器。
在不相关的注释上,以下划线开头的变量名称通常保留给编译器的内部,应在您自己的代码中避免。
有更好的方法来做垃圾收集?
是的,请勿使用new,delete
或动态内存alltodereth:
struct Players{};
struct Game{
Game(Players&& players){}
};
struct GInfo {
GInfo(Players&& players_pair):
game(std::move(players_pair)),g_thread(&Game::start, game){}
Game game;
std::thread g_thread;
};
std::list<GInfo> _actives;
void someLoop()
{
while (true) {
GInfo& ginfo = _actives.emplace_back(matchPlayers());
}
}
void garbageCollector() {
while (true) {
//Since C++20
//_active.remove_if([](GInfo& i){ return !i.game.isActive();});
//Until C++20
auto IT =std::remove_if(_actives.begin(),_actives.end(),
[](GInfo& i){ return !i.game.isActive();});
_active.erase(IT,_active.end());
//
sleep(2);
}
}
可能有一些错别字,但这就是想法。
相关文章:
- 贪婪算法编号列表
- 使用 Rcpp 加速替换迭代算法中的列表和向量元素是否合法?
- 如何使用cpp编写选择排序算法以降序对元素列表进行排序?
- 反向链接列表算法
- 修改贝尔曼-福特算法以维护基于成本的有序列表
- 关于合并两个列表的算法的一些问题
- 用于将优先级链接移动到列表标题的算法
- STD ::列表和垃圾收集算法
- 在双重链接列表中的QuickSort算法
- 使用霍尔分区方案的快速排序算法返回原始未排序列表
- 算法,用于查找获取排序列表所需的最小移动元素到结束步骤
- 从给定的 IPv6:端口列表中搜索 IPv6:端口组合的最快搜索算法是什么 O(1) 时间一致性
- 使用sort()算法C 排序对象列表
- 具有邻接列表的 Dijkstra 算法
- 针对多个列表的高效渗透算法
- dijkstra算法带有邻接列表无方向的图
- 是否有一个标准的算法来查找列表项的并集
- 算法-检查数字是否已经在列表中
- c++ 算法:搜索"列表"的子集,无序
- C++ STL 算法(列表排序)OpenMP/多线程实现