0

I need to hold std::mutex for every object that I create to use later.

I am aware of that std::mutex is non-copyable non-movable.

Also I have looked at this:

map-of-mutex-c11

What I do is, create a std::map with objectID(key) and objectInfo* , and have the mutex in objectInfo.

So, as what I store in the map is a pointer to some structure, I do not see any problem about std::mutex, which would have occured if I had, let say, std::map<int, std::mutex>.

However, it seems that the code I have written has some problem. After inserting the code with mutexes, I get sometimes segfault when I do std::unique_lock::try_lock.

Here is minimized version of the code that I inserted in huge project:

// Example program
#include <iostream>
#include <string>
#include <mutex>
#include <map>
#include <thread>

struct sObjectInfo {
    std::mutex mut;
};

std::map<int, sObjectInfo*> mymap;

void foo() {
    std::unique_lock<std::mutex> ulock(mymap[0]->mut, std::defer_lock);
    if (ulock.try_lock()) {
        std::cout << "locked.." << std::endl;
    }
    else {
        std::cout << "cant lock" << std::endl;
    }
}

int main()
{
  sObjectInfo* s1 = new sObjectInfo();
  sObjectInfo* s2 = new sObjectInfo();

  mymap[0] = s1;
  mymap[1] = s2;

  std::thread t1(foo);
  t1.join();
}

Here is the live example:

ideone

What is wrong with the use of mutexes in this example?

Mert Mertce
  • 1,049
  • 7
  • 34
  • your live example seems to work. unique_lock.try_lock only throws if the mutex is already locked, so what is your problem? – Syl Feb 23 '18 at 20:28
  • This code looks good to me and also is working when I tested it. I ran it from multiple threads and some managed to lock some failed. No exception was thrown. – PazO Feb 23 '18 at 20:30
  • Please update your question, there is nothing wrong in your code, and your live example works without segfault. – llllllllll Feb 23 '18 at 20:37
  • On a side note, you should be storing `std::unique_ptr` in your `std::map` instead of storing `sObjectInfo*`, then you don't have to worry about freeing the objects manually when you are done using the `std::map`. – Remy Lebeau Feb 23 '18 at 21:39

0 Answers0