Thread-safe stack implementation