[uClibc] Re: Is malloc thread safe?

Miles Bader miles at lsi.nec.co.jp
Wed Oct 22 02:51:25 UTC 2003


malloc does having locking code to deal with threads; however it has not
really been tested (though it's simple enough code).

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

Um, well, malloc's not part of the kernel, so of course that can
happen -- the kernel has no idea that it shouldn't switch threads.

However, it also shouldn't cause problems: the interrupted malloc should
be holding a mutex (specifically the one called `lock' in the heap data
structure; or if on a system using sbrk, __malloc_sbrk_lock).  The
interrupting thread should attempt to get this mutex, resulting in it
waiting until the first thread is done with it.

-Miles
-- 
In New York, most people don't have cars, so if you want to kill a person, you
have to take the subway to their house.  And sometimes on the way, the train
is delayed and you get impatient, so you have to kill someone on the subway.
  [George Carlin]



More information about the uClibc mailing list