How would you implement a circular buffer (ring buffer) in C? Explain how you would handle thread safety or interrupt safety.