0

How would I lock a std::mutex or boost::mutex so at the start of my method I would lock it, but regardless of it already being locked or not just proceed with the code.

I want this kind of behaviour because there is going to be 2 threads that'll be reading and writing to an std::map. When it's reading I just want to lock the mutex so that it can continue reading but the writing stops. When it's writing I want it to lock another mutex, which'll block reading and writing, and wait for both mutexes to be unlocked so that it can work with the map alone.

Jeroen
  • 15,257
  • 12
  • 59
  • 102
  • 4
    Sounds more like a job for `shared_mutex`, tbh. – Xeo Oct 16 '13 at 16:12
  • @Xeo Indeed. If you want you can put that up as an answer and I'll approve it. If not I'll put it up. – Jeroen Oct 16 '13 at 16:42
  • 2
    Use boost:shared_mutex (readers-writer lock). C++14 will implement std::shared mutex (http://en.wikipedia.org/wiki/C%2B%2B14#Shared_mutexes_and_locking) – matejk Nov 29 '13 at 13:37

0 Answers0