www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - RefCounted no thread-safety?

reply "Kira Backes" <kira.backes nrwsoft.de> writes:
Hello,


I was just looking at the source of std.typecons.RefCounted and 
it seems like it is not thread-safe at all, the count variable 
may be written concurrently from several threads which could 
cause data corruption. Is this correct or am I overlooking 
something (I’m still new to D). If it is correct then this should 
either be fixed or if it is intentional it should really be 
documented.


rgds, Kira
Jan 12 2014
next sibling parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
12-Jan-2014 23:59, Kira Backes пишет:
 Hello,


 I was just looking at the source of std.typecons.RefCounted and it seems
 like it is not thread-safe at all, the count variable may be written
 concurrently from several threads which could cause data corruption.

It cannot. In D everything is thread-local by default. And shared(RefCounted!T) would most likely fail to compile.
 Is
 this correct or am I overlooking something (I’m still new to D). If it
 is correct then this should either be fixed or if it is intentional it
 should really be documented.

-- Dmitry Olshansky
Jan 12 2014
parent reply Michel Fortin <michel.fortin michelf.ca> writes:
On 2014-01-12 20:08:17 +0000, Dmitry Olshansky <dmitry.olsh gmail.com> said:

 12-Jan-2014 23:59, Kira Backes пишет:
 Hello,
 
 
 I was just looking at the source of std.typecons.RefCounted and it seems
 like it is not thread-safe at all, the count variable may be written
 concurrently from several threads which could cause data corruption.

It cannot. In D everything is thread-local by default. And shared(RefCounted!T) would most likely fail to compile.

There's still a race when RefCounted is located in a GC-allocated memory block, as the destructor might get called from any thread. https://d.puremagic.com/issues/show_bug.cgi?id=4624 -- Michel Fortin michel.fortin michelf.ca http://michelf.ca
Jan 12 2014
parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
13-Jan-2014 00:12, Michel Fortin пишет:
 On 2014-01-12 20:08:17 +0000, Dmitry Olshansky <dmitry.olsh gmail.com>
 said:

 12-Jan-2014 23:59, Kira Backes пишет:
 Hello,


 I was just looking at the source of std.typecons.RefCounted and it seems
 like it is not thread-safe at all, the count variable may be written
 concurrently from several threads which could cause data corruption.

It cannot. In D everything is thread-local by default. And shared(RefCounted!T) would most likely fail to compile.

There's still a race when RefCounted is located in a GC-allocated memory block, as the destructor might get called from any thread. https://d.puremagic.com/issues/show_bug.cgi?id=4624

The only thing worse is that arrays of structs don't have their destructors called at all. -- Dmitry Olshansky
Jan 12 2014
prev sibling parent "Kira Backes" <kira.backes nrwsoft.de> writes:
On Sunday, 12 January 2014 at 20:08:25 UTC, Dmitry Olshansky 
wrote:
 It cannot. In D everything is thread-local by default.
 And shared(RefCounted!T) would most likely fail to compile.

Hello, thanks for the info, I did not test compiling with shared. You’re right about this, I was just too much used to C++11’s shared_ptr I think ;-)
Jan 12 2014