[plt-scheme] Creating a suspended thread

From: Eli Barzilay (eli at barzilay.org)
Date: Tue Apr 14 01:15:06 EDT 2009

On Apr 14, Marc Feeley wrote:
> 
> On 14-Apr-09, at 12:01 AM, Eli Barzilay wrote:
> 
> >>
> >> The "thread/suspend" procedure submitted by Matthew runs about 5%
> >> slower than the "thread" procedure alone (on MzScheme v4.1 + Intel
> >> Mac OS X).  So the additional semaphore operations don't seem to add
> >> much of an overhead.  But that is because thread creation in
> >> MzScheme is slow in comparison to the semaphore operations (about 45
> >> microseconds per thread creation + termination).  In a system with
> >> faster thread creation operations the overhead becomes quite
> >> noticeable (Gambit is about 45 times faster, i.e. one microsecond
> >> per thread creation + termination).
> >
> > I'm getting different numbers: I get thread creation and termination
> > at about 10 microseconds -- and that's for both versions; the
> > difference is in the noise.
> >
> > (For these two lines:
> >
> >  (time (for ([i (in-range 100000)]) (kill-thread (thread void))))
> >  (time (for ([i (in-range 100000)]) (kill-thread (thread/suspended  
> > void))))
> >
> > I'm getting these medians out of 13 runs of each:
> >
> >  first:  cpu time: 1057 real time: 1057 gc time: 123
> >  second: cpu time: 1035 real time: 1036 gc time: 148
> >
> > )
> 
> Eli, it appears that your machine is twice as fast as mine.
> Moreover it is "normal termination"

If there are different interpretations of "termination", the value in
saving such 5% is even smaller...  For example, if you interpret
termination as running:

  (time (for ([i (in-range 100000)]) (thread void)))

with no killing or waiting, then Matthew's version becomes much
faster.


> that I was measuring in my test, i.e.
> 
> (time (for ([i (in-range 100000)]) (thread-wait (thread void))))

(With Matthew's version of `thread/suspended' this will just block.
And if you're adding `thread-resume' in, then the isolation of the
single feature is even worse.)


> Aside: don't you find it strange that in your test using "thread/
> suspend" is *faster* than using "thread" even though
> "thread/suspend" calls "thread" and does a bunch of other
> operations?  That's probably worth some investigation.

It might be interesting to know why there's a difference, but if we're
getting to such minor differences then I don't care.  For any
practical use of threads that I've encountered, the cost of the real
work would make that difference insignificant.

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.