Google Groups unterstützt keine neuen Usenet-Beiträge oder ‑Abos mehr. Bisherige Inhalte sind weiterhin sichtbar.

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

5.584 Aufrufe
Direkt zur ersten ungelesenen Nachricht

climb...@gmail.com

ungelesen,
21.04.2008, 14:20:2721.04.08
an
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

ungelesen,
05.05.2008, 17:00:5705.05.08
an
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

ungelesen,
05.05.2008, 17:00:5405.05.08
an
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

ungelesen,
05.05.2008, 17:01:0105.05.08
an
> 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

ungelesen,
05.05.2008, 17:01:0805.05.08
an
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

ungelesen,
05.05.2008, 17:01:2205.05.08
an
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

ungelesen,
05.05.2008, 17:01:2005.05.08
an
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

ungelesen,
05.05.2008, 17:01:2805.05.08
an
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 neue Nachrichten