Hi,
I have this game engine where I wanted to thread object/chunk loading, so main thread doesn`t get lag spikes when new objects/2D tiles are loading.
What I wanted to do is:
- have a list of all main_objects (the objects are 2D tiles which represent terrain as grass or water)
- have an std::unordered_map, which consists of threadId and objects to be loaded/merged into main_objects later
1. Now, so I do not get iterator errors, I wanted to do that while the thread is loading new objects, it loads them into a std::unordered_map with threadId key and vector of objects.
2. When thread is finished loading, it will merge the objects from map into the main_objects vector, so objects are ready to be used and rendered.
However, this is giving me hard times, I am getting some iterator errors etc.. These are some code snippets of main logic behind this. Please, if somebody could take a look where I am doing a mistake.
1 2 3 4 5 6
|
//I wanted to make some lock here so we don`t iterate while threads are merging objects. Didn`t help though.
for (auto it = main_objects.begin(); it != main_objects.end(); it++)
{
(*it)->Update();
//Some other stuff here, as render data updates etc.
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
//The thread function which merges it`s loaded objects into main_objects so they can be used.
void Scene::MergeObjects(int threadId)
{
objectMergeLock.lock();
for (auto it = objectLoadList[threadId].begin(); it != objectLoadList[threadId].end();)
{
main_objects.push_back((*it));
it = objectLoadList[threadId].erase(it);
}
objectLoadList.erase(threadId);
objectMergeLock.unlock();
}
|
One of the errors is: vector iterators are incompatible.