Depends on if you “concurrent” readers or serialized interlaced ones. It depends on you transaction semantics - eg dirty reads, etc.
It is fairly easy for readers to grab the read lock and the writer thread (since they were added to a queue) to grab the write lock when it has work to do. Like I said, a copy on write - partitioned - can be highly effective. Review the ConcurrentHashMap source for an efficient implementation of a complex data structure that is highly concurrent for both reading and writing. If you have a tree, you would most likely partition by branches. The idea is to mutate only a portion of the structure and efficiently swap it it when ready. > On Jul 6, 2022, at 5:59 AM, Brian Candler <b.cand...@pobox.com> wrote: > > >> On Wednesday, 6 July 2022 at 11:33:29 UTC+1 ren...@ix.netcom.com wrote: >> As I said, make the mutating op simply record the desired op on a queue and >> process the serially on another thread. You are essentially implementing >> transactions. > > But how do you protect tree *readers* from having mutations take place under > their feet? You could serialize all reads as well, and copy the results. But > I don't think that's what the OP was intending. > > You could have aRWMutex, but then it's up to the API caller to obtain that > mutex *for as long as necessary* (e.g. while traversing part of the tree). > > Depending on the use case, a "functional" approach might be worth considering: > - nodes are strictly immutable > - if you need to modify a node, create a new node instead > - recursively repeat for every other node that holds a pointer to this node. > - atomically update the "tree root" pointer > > Then each reader sees a snapshot of the tree at a point in time, and garbage > collection should take care of the rest. > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/888413c1-d4ff-4752-a607-2a3f2196e1dfn%40googlegroups.com. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/B19F8B00-3FB1-411D-B48E-3EF9BC3AB839%40ix.netcom.com.