Thread-Safe Interface

Learn about the thread-safe interface.

We'll cover the following

The thread-safe interface tells us how to write interfaces when objects are used in a concurrent environment. The thread-safe interface extends the critical region to an object, meaning the object should only be used by one thread at any point in time.

Let’s see why doing this is important. Let’s suppose we have a class that has many methods inside it and then use synchronization measures (like mutex locks). This will have the following disadvantages:

  • The performance of the system is limited.
  • Deadlocks occur when methods call each other.

Let’s see how two interleaved methods calls can result in a deadlock. Suppose we have code structured like this.

Get hands-on with 1200+ tech skills courses.