Is this a good idea?

Dr.No jckj33 at gmail.com
Sat Sep 1 18:37:15 UTC 2018


On Saturday, 1 September 2018 at 17:08:25 UTC, Peter Alexander 
wrote:
> On Saturday, 1 September 2018 at 16:20:11 UTC, Dr.No wrote:
>> why move flush to outside the synchronized block?
>
> flush should be thread safe. In general, yiu want as little 
> code as possible to run under the lock. Not that important 
> though.
>
>> trying out this approach I found to be ok except in some 
>> cases, the output look like that:
>>
>>...
>>
>> also there's that extra ♪◙ character. Thos sounds memory 
>> violation somewhere.
>> This only happens when using parallel. Any guess what's 
>> possibily happeing?
>
> Hard to say without seeing code. Agree it looks like a race.

I'll try to make a reduced version of the program so that your 
guys can help me find out what's wrong.

One guess:

In the code:

> foreach(x; parallel(arr)) {
>   auto a = f(x);
>   auto res = g(a);
>   synchronized {
>    stdout.writeln(res);
>    stdout.flush();
> 
>     }

assuming res is a class type, is res's adress unique due whole 
loop execution or it can be overritten by another thread? for 
example:

the writeln() call block is locked until thread3 finish printing,
thread2 has just finished and now is waiting for thread3 free the 
resource but before that happens, thread2 just finish. Can 
thread2 overrite the res adress in any way?
I need clarifation on that to try find out what's wrong with this 
code.

for more info: g() does have the last statement as return new 
myClass()


More information about the Digitalmars-d-learn mailing list