Compiling shared example.

Peter Sommerfeld noreply at rubrica.at
Sun Oct 28 02:46:12 PDT 2012


Am 28.10.2012, 08:06 Uhr, schrieb Ali Çehreli <acehreli at yahoo.com>:

> 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

Thanks 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


More information about the Digitalmars-d-learn mailing list