www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is stdout.flush() unsafe?

reply tsbockman <thomas.bockman gmail.com> writes:
Trying to compile this:

void main()  safe
{
     import std.stdio;
     stdout.flush();
}

Fails with this message:
 Error: safe function 'main' cannot access __gshared data 
 'stdout'
Is this necessary? If so, what are the synchronization requirements for access to `stdout`?
Dec 29 2015
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 12/29/15 4:57 AM, tsbockman wrote:
 Trying to compile this:

 void main()  safe
 {
      import std.stdio;
      stdout.flush();
 }

 Fails with this message:
 Error: safe function 'main' cannot access __gshared data 'stdout'
Is this necessary? If so, what are the synchronization requirements for access to `stdout`?
Hm... what is needed is an accessor for stdout: void main() safe { import std.stdio; auto safe_stdout() trusted { return stdout; } safe_stdout.flush(); } The issue is the storage class __gshared is banned from accessing in safe code (because it is subject to races). So you have to claim to the compiler "I know this is generally not safe, but I have encapsulated it in a way to make it safe". Likely, this is what we should do for all the standard streams, not being able to access streams in safe code seems a steep restriction. -Steve
Dec 29 2015