httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Greg Stein <gst...@lyra.org>
Subject Re: [background] texts on thread safety?
Date Mon, 18 Oct 1999 12:34:55 GMT
Euh... what does this have to do with Apache?

Please consider that the Apache developers are probably *quite*
well-versed with threads and probably do not require backgrounds texts.
Heck, programming pointers are usually a bit much... instead, the standard
approach is to assume everybody knows exactly what they're doing. When
they commit stuff, *then* you comment :-)

Cheers,
-g

On Mon, 18 Oct 1999, Henrik Vendelbo wrote:
> 
> > 
> > Henrik,
> > 
> > OK, so for working variables that you write to / modify, you'd declare
> > pointers and allocate memory (presumably even for datatypes like int,
> > right)? How about constant variables (int, char, whatever)?
> > 
> 
> The problem is the same as with database transactions. Moving from one consistent state
to another.
> If one process/thread starts to changes the state while another is in the process of
changing the state of
> the state, the two will be working on data with an inconsistent state.
> To make code thread safe look for instructions that write data.
> 
> Example :
> 
> char logfile_writebuffer[2000];    // This could be allocated, still same problem
> int logfile_cursor = 0;
> 
> void buffer_logentry(request_t* request)
> {
>     if (logfile_cursor > 1700) buffer_commitbuffer();
>       
>     copy_request_to_buffer(logfile_writebuffer + logfile_cursor,request);
>     logfile_cursor += size_of_request_logentry();  
> }
> 
> What's wrong here????
> ..
> Imagine that two theads are buffering logentries almost at the same time. If the first
one is
> still executing copy_request_to_buffer when the other calls copy_request_to_buffer, the
> two will write to the same location.
> 
> So what can you do about it; Well, several things.
> 1) You could increase logfile_cursor before calling copy_request_to_buffer.
> 2) You could use a locking mechanism to ensure that no thread would call copy_request_to_buffer
>      while another is still executing copy_request_to_buffer.
> 3) You could pass a pointer to the cursor to copy_request_to_buffer, and implement it
so it
>      moves the pointer before doing anything else.
> 
> I hope it clarifies things a bit.
> 
> \Henrik
> 

--
Greg Stein, http://www.lyra.org/


Mime
View raw message