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 7078977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:36:51+00:00 2026-05-28T06:36:51+00:00

Possible Duplicate: Malloc thread-safe? I am not a bit confused while I am reading

  • 0

Possible Duplicate:
Malloc thread-safe?

I am not a bit confused while I am reading “The Linux Programming Interface”.

From the book it says that malloc is non-reentrant since it manipulates the global linked list data structure but is made thread-safe by using mutex.

I am a bit confused about this: since it’s thread-safe with using mutex and thus can be invoked by more than one threads at the same time, why it isn’t a reentrant function? (if we say that reentrant means that it can be invoked by more than one caller at the same time)

Another question is that, since malloc is thread-safe, can we put it in a signal handler?
I think the answer is yes but I am not sure since according to this book, it says that only a reentrant or async-signal-safe function can be put in the signal handler.

Can anyone explain this to me?

  • 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-05-28T06:36:51+00:00Added an answer on May 28, 2026 at 6:36 am

    if we say that reentrant means that it can be invoked by more than one
    caller at the same time

    Wrong. Reentrant means you can interrupt it and call it again before the previous incarnation ended. Imagine malloc looks like this:

    lock(mutex);
    
    /* Stuff. */
    
    unlock(mutex):
    

    What happens if it is interrupted in the middle, before unlocking and someone else calls malloc ?

    • The first context cannot continue until the second one is done
    • The second context blocks on the mutex and cannot continue until the first unlocks the mutex

    That’s a deadlock.

    Another question is that, since malloc is thread-safe, can we put it
    in a signal handler? I think the answer is yes

    Wrong again. See the example above. Imagine the main program is doing a malloc and before the function actually ends your handler calls malloc.

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

Sidebar

Related Questions

Possible Duplicate: Malloc thread-safe? I heard that glibc malloc() was not thread safe, since
Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: Singleton: How should it be used Following on from Ewan Makepeace 's
Possible Duplicate: c difference between malloc and calloc Please explain the significance of this
Possible Duplicate: When you exit a C application, is the malloc-ed memory automatically freed?
Possible Duplicate: Invalid read/write sometimes creates segmentation fault and sometimes does not I have
Possible Duplicate: C programming : How does free know how much to free? When
Possible Duplicate: Reading through file using ifstream I'm trying to find a way to
Possible Duplicate: Do I cast the result of malloc? I just learned how to

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.