Thread overview
Trying to sort shared data with a predicate causes 'unable to format shared objects'
Jun 17, 2014
George Sapkin
Jun 17, 2014
Ali Çehreli
Jun 17, 2014
George Sapkin
Jun 17, 2014
Andrew Edwards
Jun 17, 2014
George Sapkin
Jun 17, 2014
Ali Çehreli
Jun 17, 2014
George Sapkin
Jun 17, 2014
Ali Çehreli
June 17, 2014
I'm trying to sort shared data with a predicate. Buy that causes 'unable to format shared objects'. Here's an example reproducing the issue without any threading code:

shared class SomeClass {
   immutable int value;

   this(const int value) {
      this.value = value;
   }
}

void main() {
    auto n = 10;
    auto data = new shared SomeClass[n];

    foreach (i; 0..n) data[i] = new shared SomeClass(i);
    auto sorted = sort!((a, b) => a.value < b.value)(data);
}

Causes:

/usr/include/dmd/phobos/std/format.d(2616): Error: static assert  "unable to format shared objects"
/usr/include/dmd/phobos/std/conv.d(108):        instantiated from here: formatValue!(Appender!string, shared(SomeClass), char)
/usr/include/dmd/phobos/std/conv.d(879):        instantiated from here: toStr!(string, shared(SomeClass))
/usr/include/dmd/phobos/std/conv.d(282):        instantiated from here: toImpl!(string, shared(SomeClass))
/usr/include/dmd/phobos/std/conv.d(3332):        instantiated from here: to!(shared(SomeClass))
/usr/include/dmd/phobos/std/conv.d(3316):        instantiated from here: textImpl!(string, string, shared(SomeClass), string, shared(SomeClass), string, ulong, string, ulong)
/usr/include/dmd/phobos/std/algorithm.d(10352):        instantiated from here: text!(string, shared(SomeClass), string, shared(SomeClass), string, ulong, string, ulong)
/usr/include/dmd/phobos/std/algorithm.d(9068):        instantiated from here: isSorted!(__lambda1, shared(SomeClass)[])
sort.d(16):        instantiated from here: sort!((a, b) => a.value < b.value, cast(SwapStrategy)0, shared(SomeClass)[])

I'm using DMD64 2.065 on Fedora 20.

Any ideas what I'm doing wrong? Thanks.
June 17, 2014
On 06/16/2014 07:45 PM, George Sapkin wrote:
> I'm trying to sort shared data with a predicate. Buy that causes 'unable
> to format shared objects'. Here's an example reproducing the issue
> without any threading code:
>
> shared class SomeClass {
>     immutable int value;
>
>     this(const int value) {
>        this.value = value;
>     }
> }
>
> void main() {
>      auto n = 10;
>      auto data = new shared SomeClass[n];
>
>      foreach (i; 0..n) data[i] = new shared SomeClass(i);
>      auto sorted = sort!((a, b) => a.value < b.value)(data);
> }

Good news: The code compiles with 2.066 after adding 'import std.algorithm;' :)

Ali

June 17, 2014
On Tuesday, 17 June 2014 at 04:38:24 UTC, Ali Çehreli wrote:
>
> Good news: The code compiles with 2.066 after adding 'import std.algorithm;' :)
>
> Ali

Thanks, but where can I get 2.066? It seems it's not going to be branched until June 30th.

Is there any way to resolve this now with 2.065?
June 17, 2014
On 6/17/14, 11:34 AM, George Sapkin wrote:
> On Tuesday, 17 June 2014 at 04:38:24 UTC, Ali Çehreli wrote:
>>
>> Good news: The code compiles with 2.066 after adding 'import
>> std.algorithm;' :)
>>
>> Ali
>
> Thanks, but where can I get 2.066? It seems it's not going to be
> branched until June 30th.
>
> Is there any way to resolve this now with 2.065?


You create build it from source or wait until it's released in June.
June 17, 2014
Does making an array copy with shared cast away make any sense?

auto n = 10;
auto sharedData = new shared SomeClass[n];
foreach (i; 0..n) sharedData[i] = new shared SomeClass(i);

auto nonSharedData = cast(SomeClass[]) sharedData[0..$];
auto sorted = sort!((a, b) => a.value < b.value)(nonSharedData);

This seems to work but what are the implications?
June 17, 2014
On 06/17/2014 08:51 AM, George Sapkin wrote:> Does making an array copy with shared cast away make any sense?
>
> auto n = 10;
> auto sharedData = new shared SomeClass[n];
> foreach (i; 0..n) sharedData[i] = new shared SomeClass(i);
>
> auto nonSharedData = cast(SomeClass[]) sharedData[0..$];
> auto sorted = sort!((a, b) => a.value < b.value)(nonSharedData);
>
> This seems to work but what are the implications?

Shared data can be accessed by more than one thread. Unless it is locked, other threads may see the array in an inconsistent state.

Ali

June 17, 2014
On Tuesday, 17 June 2014 at 17:18:36 UTC, Ali Çehreli wrote:
> Shared data can be accessed by more than one thread. Unless it is locked, other threads may see the array in an inconsistent state.
>
> Ali

But that's a local copy of it, no? If it's not, will making a local copy solve this?
The original question still stands: how can I resolve this in 2.065? Thanks.
June 17, 2014
On 06/17/2014 11:17 AM, George Sapkin wrote:
> On Tuesday, 17 June 2014 at 17:18:36 UTC, Ali Çehreli wrote:
>> Shared data can be accessed by more than one thread. Unless it is
>> locked, other threads may see the array in an inconsistent state.
>>
>> Ali
>
> But that's a local copy of it, no? If it's not, will making a local copy
> solve this?
> The original question still stands: how can I resolve this in 2.065?
> Thanks.

As long as you know that no other thread can access the data as its being mutated, then that's fine.

There are other cases where 'shared' has to be casted-away at least for now.

Ali