digitalmars.D.learn - Compiling shared example.
- Peter Sommerfeld (36/36) Oct 26 2012 To learn about shared attribute I've copied nearly verbatim an
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (6/42) Oct 28 2012 The following two changes are the workaround at least for compilation:
- Peter Sommerfeld (9/65) Oct 28 2012 Thanks Ali, that keeps me going for now. But I wonder why I
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (27/33) Oct 28 2012 I had to look this one up. According to the spec, a shared struct does
To learn about shared attribute I've copied nearly verbatim an
example from Andreis book. The code:
import core.atomic;
struct Data{
   int value;
}
shared struct SharedStack(T) {
   private shared struct Node{
     T data;
     Node* next;
     this(T value){data = value;};
   }
   private Node* root;
   // push
   void push(T value){
     auto n = new Node(value);
     shared(Node)* oldRoot;
     do {
       oldRoot = root;
       n.next = oldRoot;
     } while(!cas(&root,oldRoot,n)); // line 30
   }
   // ...
}
SharedStack!(Data) q;
void main(string[] args){
   Data m;
   q.push(m); // line 40
}
I got the following error (dmd 2.060 win):
(40) Error function main.SharedStack!(Data).SharedStack.push(Data value)
shared is not callable using argument types (Data)
(30) template core.atomic.cas does not match any function template  
declaration
What is wrong here ?
Peter
 Oct 26 2012
On 10/26/2012 02:22 PM, Peter Sommerfeld wrote:
 To learn about shared attribute I've copied nearly verbatim an
 example from Andreis book. The code:
 import core.atomic;
 struct Data{
 int value;
 }
 shared struct SharedStack(T) {
 private shared struct Node{
 T data;
 Node* next;
 this(T value){data = value;};
 }
 private Node* root;
 // push
 void push(T value){
 auto n = new Node(value);
 shared(Node)* oldRoot;
 do {
 oldRoot = root;
 n.next = oldRoot;
 } while(!cas(&root,oldRoot,n)); // line 30
 }
 // ...
 }
 SharedStack!(Data) q;
 void main(string[] args){
 Data m;
 q.push(m); // line 40
 }
 I got the following error (dmd 2.060 win):
 (40) Error function main.SharedStack!(Data).SharedStack.push(Data value)
 shared is not callable using argument types (Data)
 (30) template core.atomic.cas does not match any function template
 declaration
 What is wrong here ?
 Peter
The following two changes are the workaround at least for compilation:
         auto n = cast(shared)new Node(value);
// ...
shared SharedStack!(Data) q;
Ali
 Oct 28 2012
Am 28.10.2012, 08:06 Uhr, schrieb Ali =C7ehreli <acehreli yahoo.com>:On 10/26/2012 02:22 PM, Peter Sommerfeld wrote:ue)To learn about shared attribute I've copied nearly verbatim an example from Andreis book. The code: import core.atomic; struct Data{ int value; } shared struct SharedStack(T) { private shared struct Node{ T data; Node* next; this(T value){data =3D value;}; } private Node* root; // push void push(T value){ auto n =3D new Node(value); shared(Node)* oldRoot; do { oldRoot =3D root; n.next =3D oldRoot; } while(!cas(&root,oldRoot,n)); // line 30 } // ... } SharedStack!(Data) q; void main(string[] args){ Data m; q.push(m); // line 40 } I got the following error (dmd 2.060 win): (40) Error function main.SharedStack!(Data).SharedStack.push(Data val=shared is not callable using argument types (Data) (30) template core.atomic.cas does not match any function template declaration What is wrong here ? PeterThe following two changes are the workaround at least for compilation:=auto n =3D cast(shared)new Node(value); // ... shared SharedStack!(Data) q; AliThanks Ali, that keeps me going for now. But I wonder why I have do declare the variables shared if the data are declared to be shared. Is that a shortcoming of the current compiler ? Anyway, I can continue ... BTW: Thanks for your book! It is of great help for beginners like me! Peter
 Oct 28 2012
On 10/28/2012 02:46 AM, Peter Sommerfeld wrote:But I wonder why I have do declare the variables shared if the data are declared to be shared. Is that a shortcoming of the current compiler ?I had to look this one up. According to the spec, a shared struct does not mean that the variables of that struct are automatically shared. It means that the members of the struct are shared: http://dlang.org/struct.html#ConstStruct Here is a test: import std.stdio; shared struct S { int i; int[] a; } void main() { auto s = S(); writeln(typeid(s)); writeln(typeid(s.i)); writeln(typeid(s.a)); } According to the output, the members are shared, not the struct: deneme.S shared(int) shared(shared(int)[])BTW: Thanks for your book! It is of great help for beginners like me! PeterThank you very much. It is really great to hear. :) Ali -- D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html
 Oct 28 2012








 
  
  
  =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com>
 =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com>