public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: Simeon Zlatarev <simeon.zlatarev@sanbolic.com>
Cc: gcc-help@gcc.gnu.org
Subject: Re: Thread safety of new and delete
Date: Tue, 08 Jan 2013 06:53:00 -0000	[thread overview]
Message-ID: <CAH6eHdQci-n0p5ZaubNHHBA32EB6arRZ1mbV1-y16aT=Pyy+og@mail.gmail.com> (raw)
In-Reply-To: <001b01cdecf9$25512860$6ff37920$@sanbolic.com>

On 7 January 2013 17:05, Simeon Zlatarev wrote:
> First, I would like to apologize if the question has been asked before. I
> tried to find answers from the web, but had no luck. (Nothing definite.)
>
> The gcc manual states, if I understand correctly with my interpretation,
> that concurrent mutation of STL objects is not thread-safe. But if the
> program never modifies the same STL objects concurrently, the compiler makes
> sure that barriers or other measures are placed in the STL code, so that the
> program works as intended. (Of course, memory and compiler barriers in the
> program's synchronization logic are still responsibility of the program's
> author.) In summary, STL is not inherently thread-safe, but is
> threads-aware.

Right.

> New and delete, probably relying on malloc and free, do mutate shared state.
> Does this mean that concurrent allocation and freeing of memory using those
> methods is unsafe? If those operations are thread-safe on multithreading
> configurations, what configuration options are necessary to enable this
> safety. Does building the gcc compiler and the libraries with one of the
> multi-threading models (like --enable-threads=posix) and then compiling the
> executable with the respective multi-threading support (like -pthread)
> sufficient?

The standard requires that new and delete operators behave as if they
do not access global state (so if they do access global state then
they must do so safely.)  See [new.delete.dataraces] in C++11.

The new in libstdc++ just forwards to malloc, so whether that
requirement is met depends on your C library's malloc.

For GNU/Linux systems (and probably most others too) you don't need to
do anything special for malloc to be thread safe.

N.B. Currently calling std::set_new_handler concurrently with calls to
new or other calls to set_new_handler *does* introduce a data race.
This is a bug in libstdc++.

> I would like to ask also, if there is a way to determine the multi-threading
> model used to compile a shared library object (like libgcc.so or
> libstdc++.so) on say, a client machine, either at run-time or during
> installation/debugging? For example, if a client has some strange behavior
> and I need to diagnose possible issues with compatibility of the shared
> objects, is there a way to interrogate the system somehow?
> (The best answer I know for the above so far, is to check the version
> information of the compiler, assuming that there is one compiler on the
> system that was built with the used shared objects.)

I can't think of a better solution than querying the compiler that
provided the library.  If the user has installed the compiler
themselves and replaced either the compiler executables or the
libraries then the answer might not be reliable.

  reply	other threads:[~2013-01-07 21:33 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-07 18:32 Simeon Zlatarev
2013-01-08  6:53 ` Jonathan Wakely [this message]
2013-01-08 12:41   ` Simeon Zlatarev
2013-01-08 13:29   ` Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH6eHdQci-n0p5ZaubNHHBA32EB6arRZ1mbV1-y16aT=Pyy+og@mail.gmail.com' \
    --to=jwakely.gcc@gmail.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=simeon.zlatarev@sanbolic.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).