How can I translate async logic with the lock to c...
# coroutines
s
How can I translate async logic with the lock to coroutines? For example. The server reads a lock(a field) async from the database to know if the client is accepted when the client pre-connects event. But the operation for aborting should execute when the connection completes event. So, the disconnect should execute when the connection is complete if the database is locked. But since the reading of the database is in
launch
it won't block the main thread, the connect complete event may be before the database reading is complete. I can add a lock in pre-connect event and wait it in complete event. How can I use coroutine to achieve this?
x
I'm not sure if I understood your problem correctly, but if you're looking for synchronization in a concurrent scenario, you should take a look into
Mutex::withLock
s
That's a way. Can I use a coroutine dispatcher and context to execute when the client connects completely but after the read? Or I have to using a lock
x
Then it seems that you're looking into having sequential code. Can't the connection and read happen in the same coroutine?
s
connect event called on the main thread and read in coroutine. Then, connect complete event called on the main thread and I want to disconnect the client here. But need to wait for read