How to share an appender!string?

Era Scarecrow via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 19 12:31:26 PDT 2016


On Thursday, 19 May 2016 at 13:33:50 UTC, Thorsten Sommer wrote:
> Issue analysis: My main issue was that the main() does not 
> waited for the new thread (I used spawn() before I opened this 
> discussion). Thus, a simple thread_joinAll(); solved that.

Since each thread can run at different times having 
thread_joinAll() would be best at the end of a loop or before 
writing the output.

An alternate to writing a custom appender is simply to make the 
assignment atomic. Haven't tried this but if you did 'shared 
string[] lines;' then you could build the string and then append 
the string to the lines. You could also avoid adding newlines 
since they would be appended afterwards by a helper function.

void writeLintes(File output) {
   foreach(ln; lines) {
     output.writeln(ln);
   }
}

  I actually wonder how much of it would have to be shared at that 
point, since strings are immutable then the returning/assigning 
strings are safe once set; The only thing that needs to be shared 
is the array that grows as the chances of reallocation.

  I'll experiment with this and get back with you. Multi-threading 
isn't my strong suit either.


More information about the Digitalmars-d-learn mailing list