Thread overview
Are constructors thread-safe?
Aug 01, 2005
Stefan
Aug 01, 2005
Stefan
Aug 01, 2005
Ben Hinkle
Aug 01, 2005
Stefan
August 01, 2005
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)

#  this()
#  {
#    // do something important here
#  }


B) (constructors are NOT thread-safe)

#  this()
#  {
#    synchronized (this)
#    {
#      // do something important here
#    }
#  }


Any comments highly appreciated!

Thanks,
Stefan



August 01, 2005
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)
>
>#  this()
>#  {
>#    // do something important here
>#  }
>
>
>B) (constructors are NOT thread-safe)
>
>#  this()
>#  {
>#    synchronized (this)
>#    {
>#      // do something important here
>#    }
>#  }
>
>
>Any comments highly appreciated!
>
>Thanks,
>Stefan
>
>
>


August 01, 2005
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)
>>
>>#  this()
>>#  {
>>#    // do something important here
>>#  }
>>
>>
>>B) (constructors are NOT thread-safe)
>>
>>#  this()
>>#  {
>>#    synchronized (this)
>>#    {
>>#      // do something important here
>>#    }
>>#  }
>>
>>
>>Any comments highly appreciated!
>>
>>Thanks,
>>Stefan
>>
>>
>>
>
> 


August 01, 2005
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)
>>>
>>>#  this()
>>>#  {
>>>#    // do something important here
>>>#  }
>>>
>>>
>>>B) (constructors are NOT thread-safe)
>>>
>>>#  this()
>>>#  {
>>>#    synchronized (this)
>>>#    {
>>>#      // do something important here
>>>#    }
>>>#  }
>>>
>>>
>>>Any comments highly appreciated!
>>>
>>>Thanks,
>>>Stefan
>>>
>>>
>>>
>>
>> 
>
>