I recently learned that compilers will optimize your code by rearranging instructions, and that this can be controlled by using barriers.
IIRC, locking a mutex makes a barrier, and unlocking a mutex also makes a barrier, to keep code inside the critical section from getting out.
So pthread_mutex_lock and pthread_mutex_unlock must implicitly be these "barriers". What if I have a class like this, which wraps my mutex?
class IMutex {
public:
virtual void lock() = 0;
virtual void unlock() = 0;
};
it seems to me, the compiler won't know that I'm calling pthread_mutex_lock() inside lock(), and pthread_mutex_unlock() inside unlock(), because it's all virtual'd away.
Will this lead to bugs? Do I need to manually specify barriers somehow?