I'm trying to make a C++ API (for Linux and Solaris) thread-safe, so that its functions can be called from different threads without breaking internal data structures. In my current approach I'm using pthread mutexes to protect all accesses to member variables. This means that a simple getter function now locks and unlocks a mutex, and I'm worried about the overhead of this, especially as the API will mostly be used in single-threaded apps where any mutex locking seems like pure overhead.
So, I'd like to ask:
- do you have any experience with performance of single-threaded apps that use locking versus those that don't?
- how expensive are these lock/unlock calls, compared to eg. a simple "return this->isActive" access for a bool member variable?
- do you know better ways to protect such variable accesses?