www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4600] New: writeln() is not thread-safe

reply d-bugmail puremagic.com writes:
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 puremagic.com
        ReportedBy: jmdavisProg gmail.com


--- Comment #0 from Jonathan M Davis <jmdavisProg 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: -------
Aug 08 2010
parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4600


Jameson <beatgammit gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |beatgammit gmail.com


--- Comment #1 from Jameson <beatgammit gmail.com> 2013-05-22 23:59:00 PDT ---
(In reply to comment #0)
 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");
 }
I couldn't reproduce this with the current DMD. This issue is quite old, so it's possibly invalid now. I'm running Linux x86_64 with DMD 2.062. Also tried ldc2 (targeting DMD 2.061), no problems. Can this be closed? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
May 22 2013