[Issue 645] New: Race condition in std.thread.Thread.pauseAll
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Mon Dec 4 00:52:53 PST 2006
http://d.puremagic.com/issues/show_bug.cgi?id=645
Summary: Race condition in std.thread.Thread.pauseAll
Product: D
Version: 0.176
Platform: PC
OS/Version: Windows
Status: NEW
Severity: normal
Priority: P2
Component: Phobos
AssignedTo: bugzilla at digitalmars.com
ReportedBy: kinaba at is.s.u-tokyo.ac.jp
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().
--
More information about the Digitalmars-d-bugs
mailing list