How to share an appender!string?

rikki cattermole via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 19 04:09:59 PDT 2016


On 19/05/2016 10:41 PM, Thorsten Sommer wrote:
> On Thursday, 19 May 2016 at 10:13:21 UTC, rikki cattermole wrote:
>
>> At this point I'd recommend you to just ignore Appender.
>> Write your own.
>
> Dear rikki,
>
> Thanks for the proposal :) Here is the new attempt #4 as simple test
> case: https://dpaste.dzfl.pl/f6a9663320e5
>
> It compiles & runs, but the array of strings gets not shared across
> threads :( I am sure that I missed something about the shared() concept...
>
> Hmm...
>
>
> Best regards,
> Thorsten

What I meant was for you to create e.g. a struct that you can control to 
meet your needs. Not to declare an empty class and make your data global.

struct MyAppender(T) {
	T[] data;
	size_t realLen;

	void add(T v) {...}
	T[] data() { return data[0 .. realLen]; }
}

void main() {
	import std.stdio : writeln;
	MyAppender!char stuff;
	stuff.add('a');
	writeln(stuff.data);
}

Although based upon your posts, I'd say you should focus more on 
learning the language and less on threading. I.e. immutable A obj = new A();
Is probably not doing what you think it is.


More information about the Digitalmars-d-learn mailing list