🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #9421

Re: Sleep() doesn't work as I'd expect

#9421
From: Alan Bellingham
Date: Mon, 12 Jul 2004 10:12
57 lines
3407 bytes
"Chris Phillips" <chris.phillips@corusgroup.com> wrote:

>If the buffer is read faster than it is written to, and frequently enough,
>then it should never occur.

Perhaps. But perhaps not. 

If the buffer writing code writes more than a single int (and I'm making
the unwarranted assumption that writing an int is atomic), it may have
problems. Assume it writes two ints:

  thread 1 - reads buffer, sees no change
  thread 2 - write first int
  thread 1 - reads buffer, sees change, reads first int, reads second
   (as yet unchanged)
  thread 2 - write second int

Here the reading thread read something which isn't what the writer
wrote.

With a mutex (or better a critical section, if your stuff is all in one
process), the result would be:

  thread 1 - locks critical section
  thread 1 - reads buffer, sees no change
  thread 1 - unlocks critical section

  thread 2 - locks critical section
  thread 2 - write first int
  thread 1 - attempts to lock critical section, but gets held out
  thread 2 - write second int
  thread 2 - unlocks critical section

  thread 1 - _finally_ locks critical section
  thread 1 - reads buffer, sees change, reads first int, reads second
  thread 1 - unlocks critical section

The writing frequency is fairly irrelevant - all that matters is whether
_when_ writing occurs, it is possible that reading may occur before it's
finished. If reading is high frequency (I infer it is) and writing is
slow (you imply it is), then it is _more_ likely, not less.

>Experience with the application to date suggests that we don't have a
>problem with losing or corrupting incoming data.

You might just be getting lucky. Or unlucky, depending on your point of
view, since corrupt data isn't always obvious.

With a circular buffer, if the last thing you're writing is where in the
buffer the latest value is, then that could be close enough to atomic
that the failure rate is almost zero. Failure rates close to zero are a
real b*gg*r to debug, since you can almost never find out where they
happen.

Alan Bellingham
-- 
ACCU Conference 2005 - 20-23 April, Randolph Hotel, Oxford, UK
Technical Details
Message-ID: <lrn4f0hhholsg4362ujbhp26kc6q1bb45r@4ax.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newshosting.com!nx01.iad01.newshosting.com!216.196.98.140.MISMATCH!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed.cwix.com!newsfeed.frii.net!newsfeed.frii.net!news.compaq.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <40eea7bc@newsgroups.borland.com> <40eeb624$1@newsgroups.borland.com> <40f24e6d@newsgroups.borland.com> <40f2522a$1@newsgroups.borland.com> <40f252c7@newsgroups.borland.com> <36l4f09t2v3s9mb9gf7g36pu0k7aojgpke@4ax.com> <40f25cf8$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,f4373c97b27dc766
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: Alan Bellingham <alanb@episys.com>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Sleep() doesn't work as I'd expect
Date: Mon, 12 Jul 2004 11:12:43 +0100
Reply-To: alanb@episys.com
Message-ID: <lrn4f0hhholsg4362ujbhp26kc6q1bb45r@4ax.com>
References: <40eea7bc@newsgroups.borland.com> <40eeb624$1@newsgroups.borland.com> <40f24e6d@newsgroups.borland.com> <40f2522a$1@newsgroups.borland.com> <40f252c7@newsgroups.borland.com> <36l4f09t2v3s9mb9gf7g36pu0k7aojgpke@4ax.com> <40f25cf8$1@newsgroups.borland.com>
X-Newsreader: Forte Agent 2.0/32.646
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 193.129.251.130
X-Trace: newsgroups.borland.com 1089627162 193.129.251.130 (12 Jul 2004 03:12:42 -0700)
Lines: 57
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newshosting.com!nx01.iad01.newshosting.com!216.196.98.140.MISMATCH!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed.cwix.com!newsfeed.frii.net!newsfeed.frii.net!news.compaq.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail