www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Shared Class Variables

reply sybrandy <sybrandy gmail.com> writes:
Evening.

I'm having a bit of a problem and I'm hoping someone can help.  I'm 
trying to create a class that is shared across threads.  The only 
purpose of this class is to write data to somewhere, though currently a 
file.  A single-threaded version of this works fine, however I can't 
seem to get the code to work correctly when dealing with multiple 
threads.  I've gotten sharing issues, compilation issues trying to 
figure out how to use a shared class correctly, and currently an 
exception occurring during class finalization.

So, my question is, what is the correct way to do this?  Would a class 
work or would a struct be better?  Perhaps a singleton?

Thanks.

Casey
May 27 2010
next sibling parent reply BCS <none anon.com> writes:
Hello sybrandy,

 Evening.
 
 I'm having a bit of a problem and I'm hoping someone can help.  I'm
 trying to create a class that is shared across threads.  The only
 purpose of this class is to write data to somewhere, though currently
 a file.  A single-threaded version of this works fine, however I can't
 seem to get the code to work correctly when dealing with multiple
 threads.  I've gotten sharing issues, compilation issues trying to
 figure out how to use a shared class correctly, and currently an
 exception occurring during class finalization.
 
 So, my question is, what is the correct way to do this?  Would a class
 work or would a struct be better?  Perhaps a singleton?
 
I don't know much about threading but I do know that D1 vs. D2 (you didn't say what version) makes a big difference. For example, in D2 globals are thread local by default where as in D1 they are not.
 Thanks.
 
 Casey
 
-- ... <IXOYE><
May 27 2010
parent sybrandy <sybrandy gmail.com> writes:
 I don't know much about threading but I do know that D1 vs. D2 (you
 didn't say what version) makes a big difference. For example, in D2
 globals are thread local by default where as in D1 they are not.
 Thanks.

 Casey
Sorry about that. I've been doing everything in D2. Casey
May 28 2010
prev sibling parent Kagamin <spam here.lot> writes:
sybrandy Wrote:

 Evening.
 
 I'm having a bit of a problem and I'm hoping someone can help.  I'm 
 trying to create a class that is shared across threads.  The only 
 purpose of this class is to write data to somewhere, though currently a 
 file.  A single-threaded version of this works fine, however I can't 
 seem to get the code to work correctly when dealing with multiple 
 threads.  I've gotten sharing issues, compilation issues trying to 
 figure out how to use a shared class correctly, and currently an 
 exception occurring during class finalization.
 
 So, my question is, what is the correct way to do this?  Would a class 
 work or would a struct be better?  Perhaps a singleton?
 
Class methods must be marked as shared and object must be stored and passed as shared. But you still must not screw up the object's data in race condition and use proper syncronization when necessary. Marking data as shared doesn't do much - it's kinda synonym for volatile.
May 28 2010