Critical sections and mutexes

David Bolen db3l at fitlinxx.com
Thu Oct 25 00:32:46 EDT 2001


<brueckd at tbye.com> writes:

> On 24 Oct 2001, David Bolen wrote:
(...)
> > Just be careful to fully document that fact in your code and avoid
> > using this sort of example for newbie questions about threaded code.
> > Otherwise what tends to happen is that someone believes that the code
> > in question is really establishing a cap of 1000 (since they won't
> > necessarily perform the same threaded analysis of the code you have)
> > and this can lead to maintenance problems down the road.
> 
> No, no, no. That just means the code has really crummy comments. Why in
> the world would "1000" show up in the comment? Comments, especially in
> Python, should be level-of-intent, so the comment for this case would be
> nearly exactly what I wrote in my post, i.e. "make sure the list doesn't
> get too big".

Read my comments again.  Nowhere do I say that 1000 shows up in the
comment.  What I said was to fully document the very "impreciseness"
that you are assuming is ok.

Let's say I'm maintaining your code.  I see a comment that says "make
sure the list doesn't get too big".  Then I read the code and I see
your comparison to 1000 (or to a constant representing 1000).  The
logical conclusion - particularly for one who hasn't been knee deep in
the threaded development - is going to be that it shouldn't exceed
that constant.  If you've written the code knowing that there's some
error margin around that constant because you didn't protect against
simultaneous access, if I were reviewing the code I'd sure want a
comment to that affect in that section of code.  That's a critical
behavior characteristic of how you are performing the check.

Someone someday later may want to change the constant (that for all
intents and purposes looks like - and probably even behaves like in
most cases - a true maximum), and expects it to really be a maximum.

Or when you write the code you know there are about 10 threads vying
for contention and you figure that overflowing by 10 is no big deal.
But then someone changes the environment and you have hundreds of
threads.  And your class got used multiple times for multiple
purposes.  All of a sudden you're significantly over memory budget
with no clear reason why.

--
-- David
-- 
/-----------------------------------------------------------------------\
 \               David Bolen            \   E-mail: db3l at fitlinxx.com  /
  |             FitLinxx, Inc.            \  Phone: (203) 708-5192    |
 /  860 Canal Street, Stamford, CT  06902   \  Fax: (203) 316-5150     \
\-----------------------------------------------------------------------/



More information about the Python-list mailing list