www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Trying to sort shared data with a predicate causes 'unable to format

reply "George Sapkin" <george.sapkin gmail.com> writes:
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.
Jun 16 2014
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Jun 16 2014
parent reply "George Sapkin" <george.sapkin gmail.com> writes:
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?
Jun 17 2014
next sibling parent Andrew Edwards <ridimz yahoo.com> writes:
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.
Jun 17 2014
prev sibling parent reply "George Sapkin" <george.sapkin gmail.com> writes:
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?
Jun 17 2014
parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Jun 17 2014
parent reply "George Sapkin" <george.sapkin gmail.com> writes:
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.
Jun 17 2014
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Jun 17 2014