Hi all,
I am struggling to understand why I am seeing the variable "buffer" clobbered when two threads are invoking the following function. My understanding says that buffer is treated as being allocated on the heap, however when reading the buffer on the other end of the socket the data is clearly clobbered.
Note the mutex is needed as two threads are sharing a common file descriptor, "fd".
If I remove the "static" keyword the program runs without error.
The buffer size is largish, approximately 800K, however nowhere near the stack size on the 64-bit SPARC architecture on which the program is run (2MB). The space consumed by other locals is negligible.
Code:
int socket_write(int fd, hdr header, int body_size, void *body)
{
static char buffer[MAX_MSG_SIZE];
int status;
pthread_mutex_lock(&fd_mutex);
// do memcpy()'s into buffer for the passed msg header and body
// and then write it out on the socket
pthread_mutex_unlock(&fd_mutex);
return status;
}