www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Are constructors thread-safe?

reply Stefan <Stefan_member pathlink.com> writes:
Are constructors thread-safe?

In other words, if want to write a thread-safe class,
what is the correct way to implement a constructor:

A) (constructors are thread-safe)







B) (constructors are NOT thread-safe)










Any comments highly appreciated!

Thanks,
Stefan
Aug 01 2005
parent reply Stefan <Stefan_member pathlink.com> writes:
Hhm, is this such a birdbrained question that no one wants to answer?
I've seen D code that does it the option B) way.

To me that appears nonsensical since IMO no other thread should be able
to obtain a reference to an object before it is fully constructed.
So I'm inclined to say that synchronization in constructors is futile as a
general rule (access to static members needs to be synchronized, of course).

Am I on the right track here? I'm new to D, so please share your thoughts :)

Kind regards,
Stefan



In article <dckp9v$23jg$1 digitaldaemon.com>, Stefan says...
Are constructors thread-safe?

In other words, if want to write a thread-safe class,
what is the correct way to implement a constructor:

A) (constructors are thread-safe)







B) (constructors are NOT thread-safe)










Any comments highly appreciated!

Thanks,
Stefan
Aug 01 2005
parent reply "Ben Hinkle" <bhinkle mathworks.com> writes:
Unless the ctor touches some static variables like adds itself to a list or 
starts a new thread or something then I agree the synchronized shouldn't be 
needed.

"Stefan" <Stefan_member pathlink.com> wrote in message 
news:dcm208$emd$1 digitaldaemon.com...
 Hhm, is this such a birdbrained question that no one wants to answer?
 I've seen D code that does it the option B) way.

 To me that appears nonsensical since IMO no other thread should be able
 to obtain a reference to an object before it is fully constructed.
 So I'm inclined to say that synchronization in constructors is futile as a
 general rule (access to static members needs to be synchronized, of 
 course).

 Am I on the right track here? I'm new to D, so please share your thoughts 
 :)

 Kind regards,
 Stefan



 In article <dckp9v$23jg$1 digitaldaemon.com>, Stefan says...
Are constructors thread-safe?

In other words, if want to write a thread-safe class,
what is the correct way to implement a constructor:

A) (constructors are thread-safe)







B) (constructors are NOT thread-safe)










Any comments highly appreciated!

Thanks,
Stefan
Aug 01 2005
parent Stefan <Stefan_member pathlink.com> writes:
In article <dcm32p$fou$1 digitaldaemon.com>, Ben Hinkle says...
Unless the ctor touches some static variables like adds itself to a list or 
starts a new thread or something then I agree the synchronized shouldn't be 
needed.
Thanks Ben, your assessment eases me a lot! Regards, Stefan
"Stefan" <Stefan_member pathlink.com> wrote in message 
news:dcm208$emd$1 digitaldaemon.com...
 Hhm, is this such a birdbrained question that no one wants to answer?
 I've seen D code that does it the option B) way.

 To me that appears nonsensical since IMO no other thread should be able
 to obtain a reference to an object before it is fully constructed.
 So I'm inclined to say that synchronization in constructors is futile as a
 general rule (access to static members needs to be synchronized, of 
 course).

 Am I on the right track here? I'm new to D, so please share your thoughts 
 :)

 Kind regards,
 Stefan



 In article <dckp9v$23jg$1 digitaldaemon.com>, Stefan says...
Are constructors thread-safe?

In other words, if want to write a thread-safe class,
what is the correct way to implement a constructor:

A) (constructors are thread-safe)







B) (constructors are NOT thread-safe)










Any comments highly appreciated!

Thanks,
Stefan
Aug 01 2005