Is it possible to elegantly craft a class that can be used as shared and as normal?

Tolga Cakiroglu tcak at pcak.com
Mon Mar 3 04:07:13 PST 2014


> Have you got a small example?

import std.stdio;

class Test{
	private int number;

	public void setNumber( int newValue ) shared{ number = newValue; 
}

	public int getNumber() shared{ return number; }
}

void main(){
	auto test = new Test();

	(cast(shared)test).setNumber( 5 );

	writeln("Value = ", (cast(shared)test).getNumber() );
}

But do not forget the fact that because of the object `test` is 
not shared, therefore the attribute `number` is not shared. Thus, 
this will not be working multi-threded.


More information about the Digitalmars-d-learn mailing list