If stdout is __gshared, why does this throw / crash?

Atila Neves via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Mar 6 01:33:11 PST 2016


On Sunday, 6 March 2016 at 01:10:58 UTC, Anon wrote:
> On Saturday, 5 March 2016 at 14:18:31 UTC, Atila Neves wrote:
>> [...]
>
> Note that `1000.iota.parallel` does *not* run 1000 threads. 
> `parallel` just splits the work of the range up between the 
> worker threads (likely 2, 4, or 8, depending on your CPU). I 
> see the effect you describe with any parallel workload. Smaller 
> numbers in place of 1000 aren't necessarily splitting things 
> off to additional threads, which is why smaller numbers avoid 
> the multi-threaded problems you are encountering.

Err, right.

>
>> [...]
>
> `File` uses ref-counting internally to allow it to auto-close. 
> `stdout` and friends are initialized in a special way such that 
> they have a high initial ref-count. When you assign a new file 
> to stdout, the ref count becomes one. As soon as one of your 
> threads exits, this will cause stdout to close, producing the 
> odd errors you are encountering on all the other threads.
>
> I would avoid reassigning `stdout` and friends in favor of 
> using a logger or manually specifying the file to write to if I 
> were you.

I see. Here's my problem: I want to make it so code not under my 
control doesn't get to write to stdout and stderr. I don't see 
any other way but to reassign stdout. Maybe I can manually bump 
up the ref count?

Atila


More information about the Digitalmars-d-learn mailing list