Reputation: 87
I have a data-structure that is not thread-safe. Multiple threads are reading and writing to that data-structure in 2 methods. (the order of calls is rather random) My approach to this problem was using a unique_lock like shown below:
struct test {
void func1() {
boost::unique_lock<boost::mutex> lock(_mutex);
// modify data-structure
}
void func2() {
boost::unique_lock<boost::mutex> lock(_mutex);
// modify data-structure
}
boost::mutex _mutex;
}
I though, that with this code, only one thread at a time is allowed to access the data, as the mutex is shared across the two methods.
But somehow may program triggers a bug in the data-structure, which I am unable to reproduce in my single-threaded test-cases...
Do I have to use the boost::unique_lock in both methods and then call
lock()
unlock()
on it?
Upvotes: 1
Views: 237
Reputation: 87
the locking mechanism, as described, was not the reason for the bug.
Well, actually it was ;)
Outside of the critical section i called
datastructure.empty()
because in an earlier version this method was threadsave.
Thanks to "Kerrek SB" for pointing on thread-sanitizer, as it's output guided me to the bug.
Upvotes: 0