Sendable and @Sendable in Swift

Updated 1 July 2022

Save

After Swift 5.5 Apple introduced the Sendable and @Sendable in Swift.

Sendable and @Sendable in Swift address the problem of type-checking values passed between concurrency constructs and actor messages.

Let’s dive into the article.

Sendable Protocol

We can pass the Sendable values from one concurrency domain to another.

For instance, you can pass a sendable value as the argument when calling an actor’s methods. Below can be marked as Sendable:

Here is an example of how integers support the Sendable protocol:

Creating a struct with a single value of type Int automatically conforms to Sendable.

If the generic type does not conform to Sendable, the compiler does not add implicit conformance.

Here is how we create a struct with conformance to Sendable.

Here is an example of enumeration.

Sendable and @Sendable in Swift

The Sendable protocol and closure indicate whether the public API of the passed values passed thread-safe to the compiler. However, it also reduces the chances of the race around conditions.

Using Sendable and @Sendable in Swift

Above all, there are cases in which the compiler does not add implicit conformance, we need to conform to the protocol manually.

For instance, the class that is immutable can conform to the Sendable protocol

In addition, the classes which are mutable need to be marked as @unchecked attribute to indicate our class is actually thread-safe due to internal locking mechanisms.

Above all, the Non-final class cannot conform to Sendable, we have to use @unchecked Sendable.

In conclusion, we can use @Sendable to tell the compiler that no extra synchronization is needed as all captured values in the closure are thread-safe.

For more details please visit here.

To read more articles please visit here.

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home