Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is malloc() function provided in <stdlib.h> thread-safe?

5,529 views
Skip to first unread message

climb...@gmail.com

unread,
Apr 21, 2008, 2:20:27 PM4/21/08
to
Hi all,
Does anyone have experience on the thread-safty issue with malloc()?
Some people said this function provided in stdlib.h is not thread-
safe, but someone said it is thread safe. Is it possible this
function evolves from thread-unsafe to thread-safe in recent years?
How could i find out?
I am using the C library coming with GNU linux distribution.

thanks a lot.

tony
--
comp.lang.c.moderated - moderation address: cl...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

Kenneth Brody

unread,
May 5, 2008, 5:00:57 PM5/5/08
to
climb...@gmail.com wrote:
>
> Hi all,
> Does anyone have experience on the thread-safty issue with malloc()?
> Some people said this function provided in stdlib.h is not thread-
> safe, but someone said it is thread safe. Is it possible this
> function evolves from thread-unsafe to thread-safe in recent years?
> How could i find out?
> I am using the C library coming with GNU linux distribution.

It depends on the implementation, so a generic "yes it is" or "no it
is not" answer can't be given. However, if the implementation itself
supports threads, the odds are it will supply a thread-safe version.
(A simple "man malloc" on your system may tell you.) I can tell you
that, on my system, if I tell the compiler I will be using threads,
it links with a thread-safe version of the runtime library, including
a thread-safe malloc().

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:ThisIsA...@gmail.com>

Thomas Richter

unread,
May 5, 2008, 5:00:54 PM5/5/08
to
climb...@gmail.com wrote:
> Hi all,
> Does anyone have experience on the thread-safty issue with malloc()?

On any *reasonable* implementation supporting threads, malloc
is thread safe, all provided you link with the proper library.

> Some people said this function provided in stdlib.h is not thread-
> safe, but someone said it is thread safe. Is it possible this
> function evolves from thread-unsafe to thread-safe in recent years?

Hardly. Having threads but a non-thread-safe version of malloc() isn't
going to be helpful.

> How could i find out?

Study your compiler manual. ANSI-C doesn't say anything about threads,
it's a POSIX extension to C.

> I am using the C library coming with GNU linux distribution.

Compile and link with -pthreads and malloc() will be thread-safe, on x86
and AMD64.

So long,
Thomas

Gordon Burditt

unread,
May 5, 2008, 5:01:01 PM5/5/08
to
> Does anyone have experience on the thread-safty issue with malloc()?

ANSI C does not have threads, so no functions are guaranteed thread-safe.
ANSI C does not guarantee that malloc() is reentrant (usable from a
signal handler).

>Some people said this function provided in stdlib.h is not thread-
>safe, but someone said it is thread safe.

WHOSE stdlib.h? It is also quite possible that malloc() is thread-safe
if you link with the thread-safe library, and not if you don't, in
the same implementation and use the same copy of stdlib.h. The same
also may apply to things like the thread-safe version of getc(), putc(),


>Is it possible this
>function evolves from thread-unsafe to thread-safe in recent years?
>How could i find out?

Functions do not evolve, especially not from VMS to Solaris. However,
it might help to read the documentation that came with your system.

> I am using the C library coming with GNU linux distribution.

I'm sure there are plenty of versions of that.

Barry Schwarz

unread,
May 5, 2008, 5:01:08 PM5/5/08
to
On Mon, 21 Apr 2008 13:20:27 -0500 (CDT), climb...@gmail.com wrote:

>Hi all,
> Does anyone have experience on the thread-safty issue with malloc()?
>Some people said this function provided in stdlib.h is not thread-
>safe, but someone said it is thread safe. Is it possible this
>function evolves from thread-unsafe to thread-safe in recent years?
>How could i find out?
> I am using the C library coming with GNU linux distribution.

Since the standard doesn't address threads at all, the answer probably
lies in the documentation for your particular system. In this case, a
gnu newsgroups seems a likely candidate where your query would be
topical.


Remove del for email

Kalle Olavi Niemitalo

unread,
May 5, 2008, 5:01:22 PM5/5/08
to
climb...@gmail.com writes:

> Does anyone have experience on the thread-safty issue with malloc()?
> Some people said this function provided in stdlib.h is not thread-
> safe, but someone said it is thread safe. Is it possible this
> function evolves from thread-unsafe to thread-safe in recent years?
> How could i find out?

The C standard does not yet specify the behaviour of threads, so
it does not say whether malloc() or any other function can safely
be called from multiple threads at the same time. However, I
would expect malloc() to be thread-safe in any implementation
that supports threads, because otherwise the callers would have
to provide their own locking and it would be unnecessarily
difficult to make independent libraries cooperate in this.

> I am using the C library coming with GNU linux distribution.

The GNU C Library claims to follow several standards, including
the Single Unix Specification, whose version 2 specifies:

# All interfaces defined by this specification will be
# thread-safe, except that the following interfaces need not be
# thread-safe:

and the list that follows does not include malloc(), which SUSv2
does define. So the GNU C Library must also have a thread-safe
malloc() in order to conform.

Unix standards may be more on topic at comp.unix.programmer though.

Dag-Erling Smørgrav

unread,
May 5, 2008, 5:01:20 PM5/5/08
to
climb...@gmail.com writes:
> Does anyone have experience on the thread-safty issue with malloc()?

There is no such thing as threads in C.

> I am using the C library coming with GNU linux distribution.

Try a GNU-related or POSIX-related forum.

DES
--
Dag-Erling Smørgrav - d...@des.no

Andrei Voropaev

unread,
May 5, 2008, 5:01:28 PM5/5/08
to
On 2008-04-21, climb...@gmail.com <climb...@gmail.com> wrote:
> Does anyone have experience on the thread-safty issue with malloc()?
> Some people said this function provided in stdlib.h is not thread-
> safe, but someone said it is thread safe. Is it possible this
> function evolves from thread-unsafe to thread-safe in recent years?
> How could i find out?
> I am using the C library coming with GNU linux distribution.

Hm. malloc() is not provided in stdlib.h, it is declared there. In fact
one can provide his own malloc implementation that will be used in place
of the one provided in libc.

The POSIX standart requires that malloc is thread-safe. So malloc
provided in glibc on Linux is thread-safe.


--
Minds, like parachutes, function best when open

0 new messages