Use closures for monitor functions to ensure thread safety