www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - DList - Various Comments/Questions

reply "Chris Williams" <yoreanon-chrisw yahoo.co.jp> writes:
Greetings,

I was attempting to use DList as a queue, for allowing data to be 
processed among a group of threads.

The first problem, here, is of course that I can't synchronize on 
a struct. This can be resolved easily enough by creating a second 
object like:

Object lock = new Object();

While I do understand that most threading is meant to be done via 
message passing, that's not really the best solution for all 
applications. Much threading is done on containers, so having 
them naturally able to be synchronized on would be useful for 
many situations. Perhaps instead of implementing the containers 
as structs or classes, it would be better to implement them as 
templates, which can be dropped into a struct or a class, 
depending on what the user needs?

I also notice that the compiler is unable to detect struct 
initializers when I try to init my DList.

E.g.:

struct SomeData {
     string a;
     string b;
}
alias DList!(SomeData) SomeList;

SomeList queue;

static this() {
     queue = SomeList([
         {"Foo", "Bar"}  // Compiler error
     ]);
}

I also noticed that this doesn't work either:

static this() {
     SomeData curr;

    curr = {"Foo", "Bar"}; // Compiler error
    SomeList.insertBack(curr);
}

And then I'm also finding that it's not acting like I would 
expect:

SomeData curr = {"Foo", "Bar"};
queue.insertBack(curr);

SomeData temp = queue.removeAny();
if (queue.empty()) {
	writeln("I'm empty!");
}
else {
	writeln("Whyyyyy?"); // This is what it prints
}

At the moment, it seems easier to write my own implementation, 
which seems unfortunate, given how central to a language its 
basic containers are.
Dec 13 2012
parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Fri, Dec 14, 2012 at 03:33:31AM +0100, Chris Williams wrote:
[...]
 I also notice that the compiler is unable to detect struct
 initializers when I try to init my DList.
 
 E.g.:
 
 struct SomeData {
     string a;
     string b;
 }
 alias DList!(SomeData) SomeList;
 
 SomeList queue;
 
 static this() {
     queue = SomeList([
         {"Foo", "Bar"}  // Compiler error
     ]);
 }
[...] This should work: queue = SomeList([ SomeData("Foo", "bar") ]); A little verbose, but it should work.
 I also noticed that this doesn't work either:
 
 static this() {
     SomeData curr;
 
    curr = {"Foo", "Bar"}; // Compiler error
[...] The usual idiom is: auto curr = SomeData("Foo", "Bar"); T -- Always remember that you are unique. Just like everybody else. -- despair.com
Dec 13 2012
parent "Chris Williams" <yoreanon-chrisw yahoo.co.jp> writes:
 This should work:

 	queue = SomeList([
 		SomeData("Foo", "bar")
 	]);

 A little verbose, but it should work.
That did indeed work. Thank you. Any thoughts on insertBack() removeAny()? Is this a bug in the implementation or am I using the container wrong, somehow?
Dec 14 2012