(we're pretty far off-topic at this point, and I should probably create a new thread, but with apologies to the OP...)Or the better option would be for this doit function to be a member function instead, and the mutex is a member variable.
That way the caller can't pass in the wrong mutex.
You've now gone the classic OOP route, and encapsulated the mutex such that the caller can never know who else has access to it. This might be fine for an inner class, or a class which is used by a single consumer. For any shared object though, this is about as bad as the global mutex.
Data is important, people. Always know where your data is, who has access to it, what operations they are performing on it. Hiding it all away inside a shared object is like sticking your head in the sand.
I'm confused.
If you already are sharing the data, why aren't you bundling the mutex with it? I can't think of any legitimate use for keeping them separate since protecting the same memory location with two separate mutexes might as well not be locked at all.
Unless I've misread something entirely.