www.digitalmars.com         C & C++   DMDScript  

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

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



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: -------
Aug 08 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4601




00:28:10 PDT ---
Actually, it looks like I'm seeing this with a single spawn sometimes, but I
haven't been able to reproduce it with short programs. Short, single-spawn
programs terminate correctly. Long single-spawn programs have had this issue
(perhaps all of them), and multi-spawn programs of any size have had it from
what I've seen. So, I have no clue what the problem is, but spawned threads are
definitely having trouble giving up the CPU to other threads, and applications
that spawn threads don't like to terminate.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 09 2010
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4601


Jonathan M Davis <jmdavisProg gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED



PDT ---
I don't know if this problem still exists or not, but I can't get the example
to fail anymore, so it at least appears that it's been fixed.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 16 2012