What's the difference between actor and @actor class

I know the difference between the actor and class which is mentioned in chapter 8.

But I have a question.

actor Base {}

in Base. all of functions should be isolated as default unless it’s marked @nonisolated
and also in the Base actor. function and properties’ access is sync. but from external access should be async.

What’s the difference with the following code

class Base {}

All of functions’s access will be based on @xxxActor??
the class Base is a thread-safety or not??

Sorry maybe Im a little bit confused.

That’s an excellent question — it honestly makes me happy when I get feedback like this because it tells me people are really reading deeply our content.

Let’s see. actor Base is an actor that uses a serial executor to serialize access and method calls. This makes it thread safe because it guarantees that no functions calls “overlap” in time — they are all executed one after another.

class Base doesn’t have the above guarantees. But — you can add it to a global actor as you point out and in this case you “borrow” the actor’s executor for instances of your class. In a way, now your class behaves similarly to an actor itself. In that sense your class is also thread-safe now because function calls will be executed serially and will never overlap in time (i.e. causing a data race or a crash).


Just wanted to add to this.

My interpretation is, one DEFINES a new serial executor through actor, the other RUNS on that serial executor.

Hope that helps.

1 Like