Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7966747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:30:53+00:00 2026-06-04T06:30:53+00:00

Does the typical malloc (for x86-64 platform and Linux OS) naively lock a mutex

  • 0

Does the typical malloc (for x86-64 platform and Linux OS) naively lock a mutex at the beginning and release it when done, or does it lock a mutex in a more clever way at a finer level, so that lock contention is reduced for concurrent calls? If it indeed does it the second way, how does it do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:30:54+00:00Added an answer on June 4, 2026 at 6:30 am

    glibc 2.15 operates multiple allocation arenas. Each arena has its own lock. When a thread needs to allocate memory, malloc() picks an arena, locks it, and allocates memory from it.

    The mechanism for choosing an arena is somewhat elaborate and is aimed at reducing lock contention:

    /* arena_get() acquires an arena and locks the corresponding mutex.
       First, try the one last locked successfully by this thread.  (This
       is the common case and handled with a macro for speed.)  Then, loop
       once over the circularly linked list of arenas.  If no arena is
       readily available, create a new one.  In this latter case, `size'
       is just a hint as to how much memory will be required immediately
       in the new arena. */
    

    With this in mind, malloc() basically looks like this (edited for brevity):

      mstate ar_ptr;
      void *victim;
    
      arena_lookup(ar_ptr);
      arena_lock(ar_ptr, bytes);
      if(!ar_ptr)
        return 0;
      victim = _int_malloc(ar_ptr, bytes);
      if(!victim) {
        /* Maybe the failure is due to running out of mmapped areas. */
        if(ar_ptr != &main_arena) {
          (void)mutex_unlock(&ar_ptr->mutex);
          ar_ptr = &main_arena;
          (void)mutex_lock(&ar_ptr->mutex);
          victim = _int_malloc(ar_ptr, bytes);
          (void)mutex_unlock(&ar_ptr->mutex);
        } else {
          /* ... or sbrk() has failed and there is still a chance to mmap() */
          ar_ptr = arena_get2(ar_ptr->next ? ar_ptr : 0, bytes);
          (void)mutex_unlock(&main_arena.mutex);
          if(ar_ptr) {
            victim = _int_malloc(ar_ptr, bytes);
            (void)mutex_unlock(&ar_ptr->mutex);
          }
        }
      } else
        (void)mutex_unlock(&ar_ptr->mutex);
    
      return victim;
    

    This allocator is called ptmalloc. It is based on earlier work by Doug Lea, and is maintained by Wolfram Gloger.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know of an easy way to re-format an NSArray as a typical
How many bytes of data does a typical HTTP get request consume. For instance
Obviously, the typical WebForms approach won't work. How does one track a user in
How can i find out how many instruction cycles does a typical wifi scan
My controllers are getting large and out of hand. A typical controller does the
I'm probably typical in being bewildered by the many syntaxes of Wpf binding. Does
How are DAOs usually designed for the typical business application ? Does one class
I have http connection code that does the typical InternetOpen -> InternetConnect -> HttpOpenRequest
Does anyone know of online course / university lectures that comprise a typical compiler
This file demonstrates a typical @installhook it does exactly the same thing as if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.