Is this a good idea?

Peter Alexander peter.alexander.au at gmail.com
Thu Aug 30 21:09:35 UTC 2018


On Thursday, 30 August 2018 at 19:59:17 UTC, Dr.No wrote:
> I would to process the current block in parallel but priting 
> need to be theread-safe so I'm using
>
>> foreach(x; parallel(arr)) {
>>    auto a = f(x);
>>    auto res = g(a);
>>    synchronized {
>>	stdout.writeln(res);
>>	stdout.flush();
>>     }
>> }
>
>
> Since f() and g() are some heavy functions, I'd like to process 
> in parallel but the printing (doesn't need to respect order but 
> must be thread-safe) hence I'm using synchronized. Is this 
> counter-productive in any way?

I don't see any problem with that assuming f and g are 
significantly more expensive than writeln. The flush can be moved 
outside the synchronized block.


More information about the Digitalmars-d-learn mailing list