Threads and stdio and HANDLE

Danny via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jan 28 03:50:45 PST 2015


Hello,

I'm trying to write some toy examples using threads in D.

Is the std.stdio.File thread-local or shared? Is flockfile used 
when I synchronize on it?

I tried checking phobos myself and found some things I don't get 
(in stdio.d):

alias FLOCK = flockfile;

this(this) { @trusted
   if(fps_)
     FLOCK(fps_);
}

What is "this(this)"?

If I want to write to stdout from a thread, do I use 
LockingTextWriter? File? shared File? Does each thread have the 
same stdout? (Ok I checked, they have the same address, so 
probably. Phobos has it as __gshared stdout, aha)

Also, in order to avoid all that (also I want to be able to set 
Console text attributes on Windows), I tried to use the lowlevel 
I/O next:

For UNIX, the fds are per-process and just integers. So I know 
there that I can just pass around the int fd to any threads.

For Windows, if I use GetStdHandle, is the resulting HANDLE valid 
for threads other than the one that called GetStdHandle ? Because 
the HANDLE is a pointer but doesn't have "shared". Does one know 
for Windows handles in general which are per-thread and which are 
per-process ?

Finally, I'm trying to come to grips with "shared":

The first use of shared is to signal to the compiler that it 
should not store the variable in thread-local storage. But when I 
acquire a lock (using "synchronized", say), I'm supposed to cast 
away the "shared", right? Does it then still know that it's not 
thread-local (but that I ensured that nobody else accesses it for 
the time being)?

What does specifying "shared class" or "shared struct" do?


More information about the Digitalmars-d-learn mailing list