[Issue 4600] New: writeln() is not thread-safe
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sun Aug 8 20:53:44 PDT 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4600
Summary: writeln() is not thread-safe
Product: D
Version: D2
Platform: Other
OS/Version: Linux
Status: NEW
Severity: normal
Priority: P2
Component: Phobos
AssignedTo: nobody at puremagic.com
ReportedBy: jmdavisProg at gmail.com
--- Comment #0 from Jonathan M Davis <jmdavisProg at gmail.com> 2010-08-08 20:53:43 PDT ---
Okay. Maybe I misunderstood something somewhere, but as I understand it,
writeln() is supposed to be thread-safe. Multiple threads can interleave lines
that they're printing, but each line that a thread prints with writeln() is
supposed to be completely printed before a line from another thread can be
printed. However, at least some of the time, the output from one thread is
mixed with another on the same line. For instance, take this program:
import std.concurrency;
import std.stdio;
void main(string[] args)
{
spawn(&func1, thisTid, args.idup);
spawn(&func2, thisTid, args.idup);
writeln("main() 1");
writeln("main() 2");
writeln("main() 3");
}
void func1(Tid parentTid, immutable string[] args)
{
writefln("func1() begin");
writefln("func1(): %s", args);
writefln("func1() end");
}
void func2(Tid parentTid, immutable string[] args)
{
writefln("func2() begin");
writefln("func2(): %s", args);
writefln("func2() end");
}
If I run it like so
./d hello world
I get:
main() 1
main() 2
main() 3
func1() beginfunc2() begin
func1() beginfunc2() begin
[./d, hello, world]
func2() end
or
main() 1
func1() beginfunc2() begin
main() 2
func1() beginfunc2() begin
main() 2
main() 3
eginfunc2() begin
main() 3
or in this case, the line printing out the args array doesn't even print the
whole beginning of the line:
main() 1
main() 2
main() 3
func1() begin
func1() begin
func2() begin
, hello, world]
func1() end
In some cases, the lines are properly printed (naturally with whole lines
potentially interleaved due to the multiple threads running), but often they
aren't. So, if writeln() is really supposed to be thread-safe, it's buggy. If
it's not, then programs intending to use writeln() (or its sibling write
functions) are going to need to wrap them with locks or some other concurrency
mechanism to being able to safely print, which certainly wouldn't play well
with the whole message-passing for concurrency paradigm.
--
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list