Process in parallel and output result to stdout theread-safely

Dr.No jckj33 at gmail.com
Mon Sep 3 18:13:37 UTC 2018


so I'im doing an expansive operation with a file, to try speed 
up, i switch to using parallel but keeping in the otuput printing 
thread-safe. But for some reason, even using synchonized, it 
doesn't work as expected, for example, it output multiples 
results on same time, as in the example below.

the code:

>	stdout.flush();
>	foreach(string fn; parallel(files))
>	{
>		auto res = doSomething(fn);
>		synchronized
>		{
>		   stdout.writefln("outjson = %s", res.serializeToJson);
>		   stdout.flush();
>		}
>
>     }


the expeced output is like that (one per line):

> outjson = {"barCode":"XXXX1","ade":"1"}
> outjson = {"barCode":"XXXX2","ade":"2"}
> outjson = {"barCode":"XXXX3","ade":"3"}
// and so on...

But it in the middle of output, I got output like this:

> outjson = {"barCode":"XXXX20","ade":"20"}♪◙outjson = 
> {"barCode":"XXXXX21","ade":"21"}

also there's that extra ♪◙ character. Thos sounds memory 
violation somewhere.
This only happens when using parallel. Any guess what's possibily 
happeing?


More information about the Digitalmars-d-learn mailing list