www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Ongoing problems with shared and immutable

reply Graham St Jack <graham.stjack internode.on.net> writes:
I have been dabbling with the shared and immutable keywords in an effort 
to find out if they are usable, and aren't getting anywhere. My current 
code base avoids them like the plague, but I can see the advantages of 
them if only they were usable.

The previous newsgroup threads on this issue have all petered out without 
any resolution.

Here is a small program that shows the sort of thing I am trying to do. 
There is nothing tricky here, just passing of immutable data via a shared 
queue between what could be two threads.



import std.typecons;


// immutable message
immutable class Message {
    public int data;

    this(int value) {
        data = value;
    }
}


// Queue-like shared synchronized container.
// The Condition on remove() is missing for brevity. 
shared class Queue {
    private Rebindable!(Message) mMsg;

    this() {
        mMsg = null;
    }

    synchronized void add(Message msg) {
        mMsg = msg;
    }
    synchronized Message remove() {
        return mMsg.get;
    }
}


int main(string args[]) {
    // pretend to be one thread queuing an object
    auto queue = new Queue();
    auto tx_message = new Message(1);
    queue.add(tx_message);

    // pretend to be another thread taking the data.
    auto rx_message = queue.remove;
    return rx_message.data;
}



Here are the error messages when I compile it with dmd 2.036:

Error: cannot implicitly convert expression (this) of type immutable
(Message) to shared_problem.Message
shared_problem.d(22): Error: function std.typecons.Rebindable!(immutable
(Message)).Rebindable.opAssign (immutable(Message) another) is not 
callable using argument types (void*) shared
Error: cannot implicitly convert expression (this) of type shared(Queue) 
to shared_problem.Queue
shared_problem.d(26): Error: function std.typecons.Rebindable!(immutable
(Message)).Rebindable.opAssign (immutable(Message) another) is not 
callable using argument types (immutable(Message)) shared
shared_problem.d(29): Error: struct std.typecons.Rebindable!(immutable
(Message)).Rebindable member original is not accessible
shared_problem.d(29): Error: struct std.typecons.Rebindable!(immutable
(Message)).Rebindable member original is not accessible


Apart from annoyances like not getting a line number on some of the 
errors, it looks like:

immutable types (at least for classes) aren't usable.

shared seems to be applied to immutable variables when it shouldn't, 
confusing the type system.

shared types don't seem to be usable.

Rebindable seems to be broken.


Note that I'm trying to use immutable and shared types in the hope that I 
can then avoid having to declare the instances of them as being immutable 
or shared everywhere. In any case, they are in the documentation, so they 
should work ;-).

Does anyone have an approach that works without bypassing shared and 
immutable completely?
Nov 26 2009
parent bearophile <bearophileHUGS lycos.com> writes:
Graham St Jack:

 I have been dabbling with the shared and immutable keywords in an effort 
 to find out if they are usable, and aren't getting anywhere.
Thank you for reminding me that tuning D for performance is premature optimization :-) Bye, bearophile
Nov 27 2009