Does an actor guarantee the same execution thread?

Viewed 58

I'm working with sqlite so I need to guarantee the thread my calls execute on, but I don't want to use the main thread. I could subclass Thread, however that introduces a host of issues trying to create async methods and executing blocks of code in the thread's main loop.

If instead I used an actor instead of a Thread subclass, will all the work within that actor be guaranteed to be on the same thread? I don't see that defined anywhere in the documentation so I'm guessing no.

1 Answers

You asked:

Does an actor guarantee the same execution thread?

No, it does not. (Neither does GCD serial queue, for that matter.)

But SQLite does not care from which thread you call it. It only cares that you don't call it from different threads simultaneously.

So, you do not have to ”to guarantee the thread my calls execute on“, but merely ensure that you don't have two threads interacting with the same connection at the same time. This is precisely the assurance that actor-isolated functions provide.

So, do not worry about what thread the actor happens to use. Only make sure you don't have simultaneous access from multiple threads at the same time.

Related