Added thread-safe message output function.