[Issue 4601] New: Spawned threads frequently don't terminate or let other threads ever run if you spawn more than one thread

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Aug 8 21:03:35 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4601

           Summary: Spawned threads frequently don't terminate or let
                    other threads ever run if you spawn more than one
                    thread
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: major
          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 21:03:34 PDT ---
In trying to use spawn() from std.concurrency, I've noticed that it's
frequently the case that once a thread starts, none of the other threads that I
have run. In some cases, another thread might take over the CPU from another
thread - it might even give it back - but rarely do all of the threads actually
complete executing. And even if they do, the application never terminates. For
instance, take the 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");
}


Aside from the issues with writeln() in bug 4600 (
http://d.puremagic.com/issues/show_bug.cgi?id=4600 ), if you run it, you'll
notice that it never terminates and that never prints the last print statement
from each thread (though whether that's an issue with writeln() followed by the
application never terminating or whether the threads are in fact not actually
running to completion, I don't know).

If I only spawn one thread, then the application seems to run just fine and
terminate properly, but if I spawn two or more, then the threads do not run to
completion, and the application does not terminate.

For spawn() and its compatriot's send() and receive() to be of any use, threads
started with spawn() obviously need to run to termination (barring infinite
loops or other such errors in the functions that the threads are running), and
the application itself needs to terminate. Unfortunately, from what I can see,
that's far from a guarantee for a moment. I don't know if I'm supposed to mark
bugs as major or worse or if only Phobos devs are supposed to do that, but this
one seems severe enough, that I'm marking it as Major. As it is, this bug makes
most cases where I would try and use multi-theading in an application
completely infeasible.

-- 
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