1

I wonder if is it ok to erase from stl:map in concurrent with find? concurrent means - two threads that try both at the same time one to erase and one to call find.

Avihai Marchiano
  • 3,837
  • 3
  • 38
  • 55
  • 2
    What do you mean by "concurrent"? One thread does `erase` while another calls `find`? That's not ok :) – jalf Aug 19 '12 at 14:08
  • Code example please. Your question is ambiguous. – moshbear Aug 19 '12 at 14:08
  • possible duplicate of [Do I need to protect read access to an STL container in a multithreading environment?](http://stackoverflow.com/questions/187583/do-i-need-to-protect-read-access-to-an-stl-container-in-a-multithreading-environ) – Bo Persson Aug 19 '12 at 14:32

2 Answers2

6

Of course not. stl::map is based on an auto-balancing binary tree (red-black usually). erase may eventually cause a heavy tree rearrangement.

Johan Råde
  • 20,480
  • 21
  • 73
  • 110
valdo
  • 12,632
  • 2
  • 37
  • 67
1

It's not okay to call a const method and a non-const method on the same object at the same time and it's not okay to call 2 non-const methods at the same time. It is, however, okay to call 2 const methods at the same time. This applies to all* const correct code, not just the standard library.

So, in your case no, it's not safe.

*Unless it's internally thread-safe, which is poor design. And nothing in the standard library is.

David
  • 27,652
  • 18
  • 89
  • 138