1

I'm trying to parallelize a simulation model using TThreads. The model objects each include amongst other things a std::map<obj*, int>. The model runs fine as long only one thread is running. If a second thread starts, I get an access violation in the map of the model of the first thread.

To test if the map is really the problem, I replaced it with a std::vector<pair<obj*, int> >. This works fine, but is much slower since I have to iterate through the entire Vector every time I try to find a specific obj*.

I know there is no possibility to reserve memory for a map, therefore I don't know how to work with the map without access violations.

JKlein
  • 31
  • 8
  • Do you *share* the map between the threads? Do you have any kind of protection against simultaneous access of the map (e.g. a mutex or a lock of some kind)? – Some programmer dude Oct 06 '17 at 08:14
  • No the map is not shared. Each thread has its own map. – JKlein Oct 06 '17 at 08:25
  • are the objects *pointed to* by these unshared maps shared amongst the threads? – Caleth Oct 06 '17 at 08:38
  • No, no memory is shared between the threads. All threads have a own model which runs completely alone. The objects in the maps are also part of the model. – JKlein Oct 06 '17 at 08:41
  • Then you have to try and create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) to show us. Otherwise this question is impossible to answer, all we can give you are wiled (and mostly wrong) guesses. If you haven't done it yet, then please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask) (and if you have read it, then please read it again). – Some programmer dude Oct 06 '17 at 08:42
  • All right then. I will try to find an Example. I just thought maybe I missed something obvious. – JKlein Oct 06 '17 at 08:46

0 Answers0