Thread overview
[Issue 1828] New: Several Thread Issues
Feb 12, 2008
d-bugmail
Feb 12, 2008
d-bugmail
Feb 28, 2008
d-bugmail
Feb 28, 2008
d-bugmail
Mar 07, 2008
d-bugmail
February 12, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1828

           Summary: Several Thread Issues
           Product: D
           Version: 2.010
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: patch
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: kinaba@is.s.u-tokyo.ac.jp


// Credit goes to http://pc11.2ch.net/test/read.cgi/tech/1202623572/30-35n

There are several problems in std.thread.
Please find attached the patch to fix all of them.

1. Thread.start
> state = TS.RUNNING;
> hdl = _beginthreadex(null, cast(uint)stacksize, &threadstart, cast(void*)this, 0, &id);
  If context-switch occurs between these two lines,
  thread which acutually is not running is treated as TS.RUNNING.
  This causes problems, for example in pauseAll().
  So these lines should be property synchronized.

> if (hdl == cast(thread_hdl)0)
  When _beginthreadex fails and this branch is taken,
  the variable nthread should be decremented.

2. std.wait timeouts
  When a call to wait() timeouts (not fails),
  > dw = WaitForSingleObject(hdl, 0xFFFFFFFF);
  > state = TS.FINISHED;
  the current implemetation treats it as it fails.
  But in this case, the thread is indeed still alive and may wake up again
  just after the "state = TS.FINISHED" statement. If that happens,
  pauseAll() do not stop the thread (because it's TS.FINISHED) and
  multiple threads may unintendedly run parallel.
  This IS a problem for std.gc, which is relying on the assumption that
  pauseAll() stops all but gc threads.

3. Priority
  It is convenient if one can set the NORMAL thread priority.


-- 

February 12, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1828





------- Comment #1 from kinaba@is.s.u-tokyo.ac.jp  2008-02-11 19:04 -------
Created an attachment (id=226)
 --> (http://d.puremagic.com/issues/attachment.cgi?id=226&action=view)
Patch to fix the issue


-- 

February 28, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1828


sjguy@cs.unc.edu changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |sjguy@cs.unc.edu




------- Comment #2 from sjguy@cs.unc.edu  2008-02-28 09:14 -------
I applied this patch and it fixed an issue I was having.  My program would eventually crash with either a "Win32 Error" or a "Error: Cannot Pause" error. This seemed to happen when my compute intensive threads were garbage collected, presumable for the exact reason Kazuhiro Inaba stated.

Since recompiling Phobos with this patch, I have not encountered either error.


-- 

February 28, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1828


spam@extrawurst.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |spam@extrawurst.org
           Severity|normal                      |critical




------- Comment #3 from spam@extrawurst.org  2008-02-28 12:14 -------
pretty please apply this patch in the next release. i am encountering these mutlithreading crashes due to the GC, too.


-- 

March 07, 2008
http://d.puremagic.com/issues/show_bug.cgi?id=1828


bugzilla@digitalmars.com changed:

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




------- Comment #4 from bugzilla@digitalmars.com  2008-03-07 00:29 -------
Fixed dmd 1.028 and 2.012 (and thanks for sorting this out)


--