[uClibc] Is malloc thread safe?
herb
herb at s7t.com
Tue Oct 21 10:04:22 UTC 2003
The original design for our project indicated that a multi-threaded app
would work best. Unfortunately (at least on my setup) threads don't seem
to work well; with malloc(). I experienced many intermittent crashes of
one of the processes that had launched threads.
Admittedly I am a relative newbie to the pthread library, and perhaps my
code is foobar. But, upon removing threading (via uClibc) from both the
client and server that are running on my HW I have yet to experience
such a crash. With threads removed the application(s) I am running are
now 100% stable.
Running a simple test with pthread and malloc _always_ crashes (on my
HW). The test code is included at the end of this message. I tried to
turn on: setenv MALLOC_DEBUG=X; to view the debug messages from the
uclibc-malloc debugging code, but the information from doing so was too
disjointed for me to follow; as it seemed that one thread would malloc
and start to print debug msgs and then in the middle of those print
statements the kernel would change the time-slice to let another
thread/process have cpu time which would attempt to alloc new memory....
My HW/SW config is as follows:
Custom HW (pertinent details):
-AT75C221 (This chip is new from Atmel. Previous boards had AT75C220 on
them but they had a bug in the SDRAM drivers which completely denied
access to SDRAM. The thread problem I am reporting, though, has appeared
on both AT75C220 with SRAM and AT75C221 with SDRAM).
-8MB SDRAM
-4MB Flash
Software(kernel etc.):
Kernel: __Very moderately__ customized, 2.4.19-uc1 uClinux
uClibc: Off the shelf: 0.9.20
<test_code>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
extern int errno;
unsigned int ewok;
void *MallocTest(void *arg)
{
int *mem;
unsigned long count = 0;
int id = ewok;
while(1)
{
mem = malloc(64);
if(!(count % 10000))
{
printf("count %d: 0x%08lx\n", id, mem);
}
free(mem);
count++;
}
}
int main()
{
pthread_t temp_thread;
pthread_create(&temp_thread, NULL, MallocTest, NULL);
ewok++;
sleep(5);
pthread_create(&temp_thread, NULL, MallocTest, NULL);
ewok++;
sleep(5);
pthread_create(&temp_thread, NULL, MallocTest, NULL);
ewok++;
sleep(5);
while(1){}
return 0;
}
</test_code>
More information about the uClibc
mailing list