开发者

How to implement a hierarchical lock or other synchronization pattern?

I have a series of logically connected collections of A, B and C objects, that are accessed by 开发者_StackOverflow中文版writer threads, reading-only threads and read-write threads.

Every A object can have several related B objects, and every B object can have several related C objects. One B object can be related to only one A object, and similarly, one C object can be related to only one B and A object. This means that when updating A, all related Bs and Cs must also be locked, and in reverse, if updating a C, the corresponding B and A are also locked?

How can i achieve the synchronization of such a structure?


when updating A, all related Bs and Cs must also be locked, and in reverse, if updating a C, the corresponding B and A are also locked

With this restriction in place you only need one mutex for each set of an A and its associated Bs and Cs. This mutex logically belongs in the A object, since there is only one of these the group, but it is probably wise to keep a reference in the B and C objects, so that it can be locked directly from there when an update must be made, without having to first traverse the object tree to find the mutex.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜