A: 

STL containers are not intrinsically thread-safe. You need to protect access to them using a synchronization mechanism. The typical ones could be a semaphore or a mutex, with the mutex being in general the faster of the two.

Amardeep
alright, thanks for the help! I'll look further into that
Brett
+1  A: 

Disclaimer: I know nothing about OpenMP specifically. However I can say that yes, two threads doing push_back (or any other modifying operation) on a list at the same time will cause problems, just the same as for a single variable.

I don't know what tools OpenMP gives you for protecting against this. Some common approaches to avoiding this problem:

  1. Put a lock (eg a mutex) around operations on the variable.
  2. Give each thread its own copy of the list, and have them remain independent. At the end of the process, as a separate step, you can merge the results from the different threads. (This is Map-Reduce, pretty much).

The second approach can give better results if you have many threads, and if it fits with your algorithm. Some algorithms can't be structured that way.

If you don't have many threads, depending on the size of your loop body, a simple lock might be the most effective solution.

Andy Mortimer
If I'm only spawning as many threads as cores on my machine, four at the moment, I assume that isn't enough threads to make the merge of the stl container worth while?... thanks for the input!
Brett
@Brett I would guess not, although it also depends on how much work you're doing in the loop per item pushed. I would go with the simple lock solution, then measure, and if it's too slow look at other approaches.
Andy Mortimer
@Andy Thanks for the help, I ended up using the lock, and it appears it rarely triggers it (hence why my program ran so many times between crashes). So I think it would only cost more time by utilizing separate data structures and combining them in the end.
Brett
agree this is the right solution. If you are using visual studio 2010 (or Intel TBB) you can use the template class combinable to use this e.g. a combinable<list<int>> for example which will give you a thread local list. then you can use the combine_each method to 'splice' them together.
Rick
@Rick sweet, I hadn't come across that. Thanks for the pointer.
Andy Mortimer