🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

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

#9281
From: Bob Gonder
Date: Mon, 12 Jul 2004 23:42
52 lines
3269 bytes
Alan Bellingham wrote:

>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.

There should be a way around that.
In my experience, a circular buffer has 2 pointers, In and Out.

The Writer reads In and Out.  If In == Out-1 (don't forget to account
for wrap), buffer overflow.
Otherwise writes to buffer[In], writes In=(In+1)mod size

The Reader reads In and Out. If ==, go away, no data.
Otherwise, reads Buffer[Out], writes Out=(Out+1)mod size.

Points:
In isn't updated until after the data is in the buffer, so Reader
doesn't attempt to read garbage data.

Out isn't written until the data has been removed, so Writer doesn't
overwrite.

>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.

Which it won't do if it doesn't know that writting is happening.
It will think nothing has come in until the writer has finished.

And if the buffer is full, the writer won't know that the reader is
attempting to empty it, so will block (or handle the overflow somehow
other than continuing to write).
With CRC verified communications, the overflow might result in dropped
data, to be caught by the CRC. In other communications, the Writter
might respond to the comm with a Nak or a Wait (just a bit before the
buffer fills, 80-90%), to stem the tide.

OTOH, if there are multiple Readers, then the Readers need to be
protected from each other.
Likewise, multiple writers from each other.
But, I would think multi-reader/writers would be confusing, as to what
data belongs to whom?
Technical Details
Message-ID: <gq66f0dk1vn948tu1o21sba1r8s6iht3fu@4ax.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!fr.ip.ndsoftware.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!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> <lrn4f0hhholsg4362ujbhp26kc6q1bb45r@4ax.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: Bob Gonder <notbg@notmindspring.invalid>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Sleep() doesn't work as I'd expect
Date: Mon, 12 Jul 2004 16:42:39 -0700
Organization: MindSpring Enterprises, Inc.
Reply-To: notbg@notmindspring.invalid
Message-ID: <gq66f0dk1vn948tu1o21sba1r8s6iht3fu@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> <lrn4f0hhholsg4362ujbhp26kc6q1bb45r@4ax.com>
X-Newsreader: Forte Agent 1.93/32.576 English (American)
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 65.40.148.194
X-Trace: newsgroups.borland.com 1089675757 65.40.148.194 (12 Jul 2004 16:42:37 -0700)
Lines: 55
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!fr.ip.ndsoftware.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail