dmd 1.047 and 2.032 releases
Graham St Jack
graham.stjack at internode.on.net
Sun Sep 6 14:50:36 PDT 2009
Its great to see so many bugs being sorted out.
However, I am having all kinds of trouble with "shared", which up until
now I have been able to fairly easily sidestep. Here is a cut-down
example of what I am trying to do, which is to have one thread acquiring
data and passing it on to another thread via a queue which uses
appropriate synchronization mechanisms to make things thread-safe. The
data passed through is immutable, so theoretically everything should be
fine.
This kind of thing is very routine in multi-threaded programs, and should
be easy to pull off. I find I have to do heaps of nasty casting to get
past the compiler (the immutable assignment thing is a side issue).
Is there a neater syntax for this? How about being able to say "shared
immutable class Message {...}" (or just shared or just immutable or just
const), and then all instances (new, local, parameter, return) would
automatically be shared and immutable?
class Message {
int data_;
this(int value) {
data_ = value;
}
int get() immutable {
return data_;
}
}
// queue-like synchronized container
class Queue {
immutable(Message) msg_;
this() {
msg_ = null;
}
synchronized void add(immutable(Message) msg) {
Message * tmp = cast(Message *) &msg_;
*tmp = cast(Message) msg;
}
synchronized immutable(Message) remove() {
return msg_;
}
}
int main(string args[]) {
// pretend to be one thread queueing an object
auto queue = cast(shared) new Queue();
auto message = cast(immutable) cast(shared) new Message(1);
queue.add(message);
// pretend to be another thread taking the data and working with it
return queue.remove.get;
}
More information about the Digitalmars-d-announce
mailing list