httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Henrik Vendelbo" <>
Subject Re: [background] texts on thread safety?
Date Mon, 18 Oct 1999 12:15:03 GMT

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


View raw message