Bug 4601 – Spawned threads frequently don't terminate or let other threads ever run if you spawn more than one thread
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2010-08-08T21:03:00Z
Last change time
2012-10-16T20:35:44Z
Assigned to
nobody
Creator
issues.dlang
Comments
Comment #0 by issues.dlang — 2010-08-08T21:03:34Z
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.
Comment #1 by issues.dlang — 2010-08-09T00:28:10Z
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.
Comment #2 by issues.dlang — 2012-10-16T20:35:44Z
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.