Thread overview
RefCounted no thread-safety?
Jan 12, 2014
Kira Backes
Jan 12, 2014
Dmitry Olshansky
Jan 12, 2014
Michel Fortin
Jan 12, 2014
Dmitry Olshansky
Jan 12, 2014
Kira Backes
January 12, 2014
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
January 12, 2014
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
January 12, 2014
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

January 12, 2014
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 ;-)
January 12, 2014
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
>
Awful.
The only thing worse is that arrays of structs don't have their destructors called at all.

-- 
Dmitry Olshansky