www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Right way to share object through threads

reply "Andrea Fontana" <nospam example.com> writes:
I can't understand which is the right way to share MyClass thru 
threads.

MyClass performs a lot of operation during init (it has to parse 
a lot of data). After this long init, it runs very fast 
operations so I want to build it just one time and use it on 
different threads.

Declaring it as a global object in this way:

class MyClass
{
	this() {}
}

shared MyClass test;

when i try to use contructor, dmd says:

Error: cannot implicitly convert expression (new MyClass) of type 
test.MyClass to shared(MyClass)

Which is the right way to implement it?
May 10 2012
next sibling parent sclytrack <sclytrack iq87.fr> writes:
On 05/10/2012 01:12 PM, Andrea Fontana wrote:
 I can't understand which is the right way to share MyClass thru threads.

 MyClass performs a lot of operation during init (it has to parse a lot
 of data). After this long init, it runs very fast operations so I want
 to build it just one time and use it on different threads.

 Declaring it as a global object in this way:

 class MyClass
 {
 this() {}
 }

 shared MyClass test;

 when i try to use contructor, dmd says:

 Error: cannot implicitly convert expression (new MyClass) of type
 test.MyClass to shared(MyClass)

 Which is the right way to implement it?

new shared(MyClass)();
May 10 2012
prev sibling parent "Andrea Fontana" <nospam example.com> writes:
On Thursday, 10 May 2012 at 11:42:44 UTC, sclytrack wrote:
 On 05/10/2012 01:12 PM, Andrea Fontana wrote:
 I can't understand which is the right way to share MyClass 
 thru threads.

 MyClass performs a lot of operation during init (it has to 
 parse a lot
 of data). After this long init, it runs very fast operations 
 so I want
 to build it just one time and use it on different threads.

 Declaring it as a global object in this way:

 class MyClass
 {
 this() {}
 }

 shared MyClass test;

 when i try to use contructor, dmd says:

 Error: cannot implicitly convert expression (new MyClass) of 
 type
 test.MyClass to shared(MyClass)

 Which is the right way to implement it?

new shared(MyClass)();

Thank you! I didn't know this syntax!
May 10 2012