@@ -, +, @@ When a thread leaves, arena_thread_freeres is called, the malloc A common problem can be described as: free_list->next_free = free_list; When a program has several short lived threads, and most commonly --- malloc/arena.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) --- a/malloc/arena.c +++ a/malloc/arena.c @@ -935,8 +935,26 @@ arena_thread_freeres (void) if (a != NULL) { (void) mutex_lock (&list_lock); - a->next_free = free_list; - free_list = a; + /* Use other arenas before "a". */ + if (a == free_list) + free_list = free_list->next_free; + /* Insert "a" last in free_list. */ + if (free_list != NULL) + { + mstate b = free_list; + while (b->next_free != NULL) + { + /* Make sure to check if "a" is already in free_list. */ + if (b->next_free == a) + b->next_free = a->next_free; + else + b = b->next_free; + } + b->next_free = a; + } + else + free_list = a; + a->next_free = NULL; (void) mutex_unlock (&list_lock); } } --