www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Concurrency send immutable

reply SrMordred <patric.dexheimer gmail.com> writes:
immutable int[] arr = [1,2,3,4,5];
auto t = spawn({ receive( (immutable int[] v) => writeln(v) );});
t.send(arr);

whats the problem here?
Nov 24 2017
parent reply SrMordred <patric.dexheimer gmail.com> writes:
On Friday, 24 November 2017 at 12:05:16 UTC, SrMordred wrote:
 immutable int[] arr = [1,2,3,4,5];
 auto t = spawn({ receive( (immutable int[] v) => writeln(v) 
 );});
 t.send(arr);

 whats the problem here?
Nothing prints out
Nov 24 2017
next sibling parent reply Daniel Kozak <kozzi11 gmail.com> writes:
Should print something like this:
std.concurrency.OwnerTerminated std/concurrency.d(223): Owner terminated

Because main thread is terminated, because types do not match

this will work
import std.stdio;
import std.concurrency;

void fun()
{
receive( (immutable (int)[] v) => writeln(v) );
}

int main(string[] args)
{
immutable int[] arr = [1,2,3,4,5];
auto t = spawn(&fun);
t.send(arr);
return 0;
}

On Fri, Nov 24, 2017 at 1:06 PM, SrMordred via Digitalmars-d-learn <
digitalmars-d-learn puremagic.com> wrote:

 On Friday, 24 November 2017 at 12:05:16 UTC, SrMordred wrote:

 immutable int[] arr = [1,2,3,4,5];
 auto t = spawn({ receive( (immutable int[] v) => writeln(v) );});
 t.send(arr);

 whats the problem here?
Nothing prints out
Nov 24 2017
parent reply SrMordred <patric.dexheimer gmail.com> writes:
On Friday, 24 November 2017 at 12:36:42 UTC, Daniel Kozak wrote:
 Should print something like this: 
 std.concurrency.OwnerTerminated std/concurrency.d(223): Owner 
 terminated
Yes, it was, I was aware of this and put some sleep after that too.
 (immutable (int)[] v)
OK that parenteshis was the problem, thanks :) But its a little confusing i think, since the syntax for creating the array is different from the syntax on the arguments. and: immutable int[]; immutable (int)[]; immutable (int[]); (differences? what/when use) And other question: When you send immutable data , i'm getting a copied data on the other thread, or its the same data? Like: send(thread1, arr); send(thread2, arr); send(thread3, arr); send(thread4, arr); (multiple copies, or all threads are seeing the same addresses ? )
Nov 24 2017
parent reply drug <drug2004 bk.ru> writes:
24.11.2017 15:53, SrMordred пишет:
 On Friday, 24 November 2017 at 12:36:42 UTC, Daniel Kozak wrote:
 Should print something like this: 
 std.concurrency.OwnerTerminated std/concurrency.d(223): Owner terminated
Yes, it was, I was aware of this and put some sleep after that too.
 (immutable (int)[] v)
OK that parenteshis was the problem, thanks :) But its a little confusing i think, since the syntax for creating the array is different from the syntax on the arguments. and: immutable int[]; immutable (int)[]; immutable (int[]); (differences? what/when use)
Yes, it's different. immutable int[] is equal to immutable (int[]) and means immutable slice of immutable int. on the other hand immutable (int)[] means _mutable_ slice of immutable int.
 
 And other question:
 
 When you send immutable data , i'm getting a copied data on the other 
 thread, or its the same data?
 
 Like:
 
 send(thread1, arr);
 send(thread2, arr);
 send(thread3, arr);
 send(thread4, arr);
 
 (multiple copies, or all threads are seeing the same addresses ? )
the same data, it's the purpose of immutable
Nov 24 2017
parent SrMordred <patric.dexheimer gmail.com> writes:
Nice, thank you!
Nov 24 2017
prev sibling parent Daniel Kozak <kozzi11 gmail.com> writes:
import std.stdio;
import std.traits;

int main(string[] args)
{
immutable int[] arr = [1,2,3,4,5];
writeln(ImplicitConversionTargets!(typeof(arr)).stringof);
return 0;
}

On Fri, Nov 24, 2017 at 1:36 PM, Daniel Kozak <kozzi11 gmail.com> wrote:

 Should print something like this:
 std.concurrency.OwnerTerminated std/concurrency.d(223): Owner terminated

 Because main thread is terminated, because types do not match

 this will work
 import std.stdio;
 import std.concurrency;

 void fun()
 {
 receive( (immutable (int)[] v) => writeln(v) );
 }

 int main(string[] args)
 {
 immutable int[] arr = [1,2,3,4,5];
 auto t = spawn(&fun);
 t.send(arr);
 return 0;
 }

 On Fri, Nov 24, 2017 at 1:06 PM, SrMordred via Digitalmars-d-learn <
 digitalmars-d-learn puremagic.com> wrote:

 On Friday, 24 November 2017 at 12:05:16 UTC, SrMordred wrote:

 immutable int[] arr = [1,2,3,4,5];
 auto t = spawn({ receive( (immutable int[] v) => writeln(v) );});
 t.send(arr);

 whats the problem here?
Nothing prints out
Nov 24 2017