How do you ensure thread safety when modifying a shared resource across multiple background threads in a mobile application?