www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - is it possible synchronized(null) ? i.e NO-OP

reply mw <mingwu gmail.com> writes:
Hi,

I haven't tried, but can I do:

```
void foo(lots of params) {
   Object lock = (a particular condition) ? realLock : null;

   synchronized(lock) {
     // lots of complex code block here
   }

}
```

i.e depending on a a particular condition, the complex code block 
either need to be sync-protected, or not needed.

Method foo has lots of params, I try to avoid refactor the inner 
code block into a separate method.

So my question: is it possible synchronized(null)? essentially 
means NO-OP.

If it's not currently supported, can we add such feature?

Thank.
Aug 26 2022
parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On the main forum, Paul Backus proposed a nested function as well as a 
scoped lock.

On 8/26/22 10:13, mw wrote:

    Object lock = (a particular condition) ? realLock : null;
And I want to point out that "a particular condition" must not change between the check above and the following synchronized statement.
    synchronized(lock) {
      // lots of complex code block here
    }
Ali
Aug 26 2022