I think Martin wants a solution for the problem
a->lock() { // Some code };
b->lock() { // Some other code };
a->lock() { // Even more code };
While following is possible, it's a dull sword:
void kludge(int(0..1) x) { synchronized { if(x) // Some code else // Even more code } }
kludge(0); synchronized { // Some other code } kludge(1);
/ Martin Nilsson (Åskblod)
Previous text:
2003-02-05 02:20: Subject: synchronized
The benefit is that it's very clean and amazingly clear what it does (I don't understand your "so I can tell easily which regions are synchronized with each other" comment. What is hard?
The synchronized method format makes it so that the format is never accessed by more than one thread at any time. Synchronizing on a variable makes it the implicit "lock" (i.e anything else synchronizing on that variable will get a lock).
If you read more in the thread you'll see a construct which exactly handles the 'syncronized(variable) { ... }' case.
The whole idea is to avoid having to explicedly define mutex variables, which you often need to keep in global variables. If you always had to specify a mutex, that would remove half the purpose.
That said a implicit lambda construct I wrote earlier that uses a weak mapping can check to see if the variable sent to it is a mutex and if is use that rather than another entry in the weak mapping.
/ David Hedbor