How can I implement shared and exclusive access for reading and writing? #6
Replies: 2 comments
-
|
I plan to add phase-fair readers-writer locks in the future, but not in the near future. Here is the expected content of 0.15.0:
Since I now see that readers-writer locks are really needed by more than just me, I will raise the priority and add them in one of the next versions after 0.15.0:
After that, you should be able to use readers-writer locks in both threads and tasks. However, that would not be enough to support checkpoints, and there may (or may not) be deadlocks when using two different APIs in the same thread. |
Beta Was this translation helpful? Give feedback.
-
|
I think it makes sense to create a non-aiologic primitive so you do not have to wait too long. Theoretically (and, as I will show below, practically too), readers-writer locks can be derived from reentrant locks. Let the acquire methods support identifier passing. Then we have possible situations where different tasks pass the same identifier. How to handle such situations?
What is curious is that we have actually invented a higher-level primitive. Instead of a waiting queue consisting of events (items of type In my opinion, the concept of groups shows what we are dealing with quite well. It is similar to groups in UNIX: a file can be owned by a personal group (the group identifier is equal to the user identifier), or it can be owned by a group of several users, in which case all of them can work with the file. I said that readers-writer locks can be derived from reentrant locks. And indeed, using a shared identifier for readers would allow them to work simultaneously, while writers could be given no shared identifier and then work separately. But I actually derived a more general primitive: if there are only two phases (reader and writer) in readers-writer locks, there can be as many phases as you like in I have already implemented Here is how you can use base = GLock() # group-level!
reading = GLock(base, default_group="reading")
writing = GLock(base)
with reading: # for sync reader
...
async with writing: # for async writer
...It is also useful when your code is coroutine-safe but not thread-safe, and you want to synchronize thread-level access without blocking the event loop. To do this, just pass lock = GLock(default_group_factory=aiologic.lowlevel.current_thread_ident) # thread-level!Note that in |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
How to allow multiple threads to read a shared resource concurrently while ensuring exclusive access for threads that need to modify it?
It's a very popular question, so I decided that it should be discussed.
In C++ I can do this:
How can I do this with aiologic in the most appropriate way?
Also, it makes sense to consider not only threads but also async tasks.
Beta Was this translation helpful? Give feedback.
All reactions