How to have multiple synchronized shared types. Programming in D, page 647

Brother Bill brotherbill at mail.com
Sun Aug 31 01:49:34 UTC 2025


On Sunday, 31 August 2025 at 01:34:18 UTC, Steven Schveighoffer 
wrote:
> 
> The correct way to do this is a nested synchronized statement:
>
> ```d
> synchronized(from) synchronized(to)
> {
>    ...
> }
> ```
>
> -Steve

This code compiles:
```
import std.stdio;

void main()
{

}

class BankAccount
{

}

void transferMoney(shared BankAccount from,	shared BankAccount to)
{
	synchronized (from) synchronized (to)   // ← correct
	{
		// ...
	}
}

```


More information about the Digitalmars-d-learn mailing list