Bug 645 – Race condition in std.thread.Thread.pauseAll
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2006-12-04T02:52:00Z
Last change time
2014-02-15T13:28:49Z
Assigned to
bugzilla
Creator
kinaba
Comments
Comment #0 by kinaba — 2006-12-04T02:52:52Z
Line 334 and 335 @ phobos/std/thread.d
> if (t && t !is tthis && t.state == TS.RUNNING)
> t.pause();
The thread t may change its state before t.pause() and
after t.state == TS.RUNNING. For example, it may finish running.
If that happens, an exception is thrown:
> Error: Thread error: cannot pause
and thus the whole execution of pauseAll() fails.
But IMHO pauseAll should not fail.
The situation can be reproduced by the following code.
------------------------
// credit goes to http://f17.aaa.livedoor.jp/~labamba/?BugTrack%2F26
import std.thread;
class DoNothing : Thread // threads that does nothing
{
int run() { return 0; }
}
class StartAndTerminateLoop : Thread
{ // infinitely starting and terminating threads
int run()
{
for(;;) {Thread t=new DoNothing; t.start; t.wait;}
return 0;
}
}
void main()
{
(new StartAndTerminateLoop).start;
for(;;) { Thread.pauseAll; Thread.resumeAll; }
// infinitely repeat pauseAll/resumeAll
// and eventually triggers the race hazard
}
------------------------
One way to solve this problem is providing a private
non-throwing pause() function and using it in pauseAll().
Comment #1 by braddr — 2007-10-27T11:59:44Z
Please see the patch attached to bug 318. It should fix this bug as well.