On Saturday, 17 November 2012 at 13:22:23 UTC, Michel Fortin wrote:
On 2012-11-16 18:56:28 +0000, Dmitry Olshansky <[email protected]> said:

11/16/2012 5:17 PM, Michel Fortin пишет:
In case you want to protect two variables (or more) with the same mutex.
For instance:

    Mutex m;
    synchronized(m) int next_id;
    synchronized(m) Object[int] objects_by_id;


Wrap in a struct and it would be even much clearer and safer.
struct ObjectRepository {
        int next_id;
        Object[int] objects_by_id;
}
//or whatever that combination indicates anyway
synchronized ObjectRepository objeRepo;

I guess that'd be fine too.

<snip>

That solution does not work in the general case. More specifically any graph-like data structure. E.g a linked-lists, trees, etc..
Think for example an insert to a shared AVL tree.

Reply via email to