InterlockedExchangeAdd is a Windows API call to update an integer under a
locked memory situation, so it's just like a critical section, but much
faster.

It's simply adding and subtracting to/from FBufferSize which is used to
limit the amount of memory written by WriteBuf to keep RAM usage under
control.  So it's nothing to do with that.

Ross.

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Chris Stebbing
Sent: Saturday, 4 August 2007 20:39
To: Borland's Delphi Discussion List
Subject: Re: Threading issue

Hi Ross,

 >  The read and write
>routines should be accessing different memory buffers at all times, and 
>this seems to be the case when no seek is done.  It all works fine.

This sort of assumption is dangerous when dealing with threads.  :-)

Without knowing what
     InterlockedExchangeAdd(@FBufferSize,Len);
does, but suspecting that this is where the buffer information is updated
and stored, then I suspect that this is the source of your problem.

I would suggest that you put the same critical section around this call only
in both the read and write routines.  This allows both to do most of their
work independantly, but forces them to converse before accessing the buffer
information.

Someone once explained to me that even if it's a single byte of information
that is exchanged, you have to assume that this memory location is undefined
in the middle of being updated, and that it could be at just this instant of
time that the threads are swapped.  Assuming such has always stood me in
good stead when writing threaded code.

Cheers,
Chris.


_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to