www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - how to use shared keyword in 2.063 version?

reply "Andrey" <vangelisforever yandex.ru> writes:
Hello!

I'm trying to use following code:

<======================================>
//...

class A
{
	private
	{
		int m_someVar = 10;
	}

	public
	{
		this()
		{
		}
	}
}


int main(string[] args)
{
	shared A a = null;
	a = new shared(A)(); // error.

	return 0;
}
<======================================>

And on compile time, the compiler says "Error: non-shared method 
main.A.this is not callable using a shared object".
How can I use an objects as shared, which classes were not 
defined with "synchronized" or "shared" keyword?


Thanks.
May 31 2013
parent reply "Anthony Goins" <neontotem gmail.com> writes:
On Friday, 31 May 2013 at 21:01:49 UTC, Andrey wrote:
 Hello!

 I'm trying to use following code:

 <======================================>
 //...

 class A
 {
 	private
 	{
 		int m_someVar = 10;
 	}

 	public
 	{
 		this()
 		{
 		}
 	}
 }


 int main(string[] args)
 {
 	shared A a = null;
 	a = new shared(A)(); // error.

 	return 0;
 }
 <======================================>

 And on compile time, the compiler says "Error: non-shared 
 method main.A.this is not callable using a shared object".
 How can I use an objects as shared, which classes were not 
 defined with "synchronized" or "shared" keyword?


 Thanks.
To create a shared object you need shared this ctor. immutable this() for immutable, and const this() for const.
May 31 2013
parent reply "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Friday, May 31, 2013 23:26:19 Anthony Goins wrote:
 To create a shared object you need shared this ctor.
 
 immutable this() for immutable,
 
 and const this() for const.
 

Either that or you create it as thread-local and cast to shared. - Jonathan M Davis
May 31 2013
parent reply "Andrey" <vangelisforever yandex.ru> writes:
On Saturday, 1 June 2013 at 00:58:00 UTC, Jonathan M Davis wrote:
 On Friday, May 31, 2013 23:26:19 Anthony Goins wrote:
 To create a shared object you need shared this ctor.
 
 immutable this() for immutable,
 
 and const this() for const.
 

Either that or you create it as thread-local and cast to shared. - Jonathan M Davis
Does it mean, that to create shared Mutex or shared Socket for example, I have to use next construction: shared Socket socket = cast(shared Mutex)(new Socket()); shared Mutex m = cast(shared Mutex)(new Mutex()); ??
Jun 01 2013
parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday, June 01, 2013 10:03:28 Andrey wrote:
 On Saturday, 1 June 2013 at 00:58:00 UTC, Jonathan M Davis wrote:
 On Friday, May 31, 2013 23:26:19 Anthony Goins wrote:
 To create a shared object you need shared this ctor.
 
 immutable this() for immutable,
 
 and const this() for const.
 

Either that or you create it as thread-local and cast to shared. - Jonathan M Davis
Does it mean, that to create shared Mutex or shared Socket for example, I have to use next construction: shared Socket socket = cast(shared Mutex)(new Socket()); shared Mutex m = cast(shared Mutex)(new Mutex());
Given the lack of shared constructors, yes - though you should probably write it more like auto mutex = cast(shared)new Mutex; auto socket = cast(shared)new Socket; since then you don't have to worry about accidentally changing the base type (like you did with the Socket), and you don't have to write the type multiple times. - Jonathan M Davis
Jun 01 2013
parent "Andrey" <vangelisforever yandex.ru> writes:
On Saturday, 1 June 2013 at 16:00:58 UTC, Jonathan M Davis wrote:
 On Saturday, June 01, 2013 10:03:28 Andrey wrote:
 On Saturday, 1 June 2013 at 00:58:00 UTC, Jonathan M Davis 
 wrote:
 On Friday, May 31, 2013 23:26:19 Anthony Goins wrote:
 To create a shared object you need shared this ctor.
 
 immutable this() for immutable,
 
 and const this() for const.
 

Either that or you create it as thread-local and cast to shared. - Jonathan M Davis
Does it mean, that to create shared Mutex or shared Socket for example, I have to use next construction: shared Socket socket = cast(shared Mutex)(new Socket()); shared Mutex m = cast(shared Mutex)(new Mutex());
Given the lack of shared constructors, yes - though you should probably write it more like auto mutex = cast(shared)new Mutex; auto socket = cast(shared)new Socket; since then you don't have to worry about accidentally changing the base type (like you did with the Socket), and you don't have to write the type multiple times. - Jonathan M Davis
Thank you! Now my app works fine.
Jun 03 2013