public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Hei Chan <structurechart@yahoo.com>
To: Ian Lance Taylor <iant@google.com>
Cc: "gcc-help@gcc.gnu.org" <gcc-help@gcc.gnu.org>
Subject: Re: Sequenced-Before for g++ 4.1.2?
Date: Tue, 17 Apr 2012 03:04:00 -0000	[thread overview]
Message-ID: <1334631810.73884.YahooMailNeo@web36508.mail.mud.yahoo.com> (raw)
In-Reply-To: <mcripgzws9k.fsf@dhcp-172-18-216-180.mtv.corp.google.com>

Hi Ian,

I probably misinterpreted what you meant about "globally visible memory".  I thought you meant that all threads would see the same copy.  What does "globally visible memory" refer to?

I didn't do anything to prevent anyone from accessing the map because I thought what was changed before pthread_create() would be visible by the newly created thread.  Maybe it has nothing to do with the cache but an incomplete object..


Cheers,
Hei



----- Original Message -----
From: Ian Lance Taylor <iant@google.com>
To: Hei Chan <structurechart@yahoo.com>
Cc: "gcc-help@gcc.gnu.org" <gcc-help@gcc.gnu.org>
Sent: Monday, April 16, 2012 9:33 AM
Subject: Re: Sequenced-Before for g++ 4.1.2?

Hei Chan <structurechart@yahoo.com> writes:

> If the compiler will issue all writes to globablly visible memory
> before pthread_create() is called, isn't it true that it doesn't
> matter whether pthread library would have a memory barrier during
> pthread_create?

No.  You have to consider caching effects.  With no memory barriers at
all the new thread, running on a different processor, may not see the
changes written to memory by the first processor.


> I am running into an odd issue that I added a key-value pair to
> std::map<long, Foo*> (a member variable) in a constructor, and then
> called pthread_create() in the constructoras well, but when the new
> thread tried to look up one of the key-value pairs, I got a segfault
> inside std::map<long, Foo*>::find().  I could only think of when I
> tried to read the map, and the OS was trying to make the previous
> writes to the map visible to the new thread.  And such crash only
> happened 1% of the time.
>
>
> Nowhere in my code changes the map except inside the constructor, and
> I printed out the key before I inserted it into the map.  When I ran
> gdb, I saw the key value and I did see the key in the log as well.

Do you do anything to ensure that the new thread does not access the map
until the constructor has completed?  I think that such a lock would be
required.  Other than that, I think we would have to see a test case.

Ian


> ----- Original Message -----
> From: Ian Lance Taylor <iant@google.com>
> To: Hei Chan <structurechart@yahoo.com>
> Cc: "gcc-help@gcc.gnu.org" <gcc-help@gcc.gnu.org>
> Sent: Friday, April 13, 2012 10:46 PM
> Subject: Re: Sequenced-Before for g++ 4.1.2?
>
> Hei Chan <structurechart@yahoo.com> writes:
>
>> I am still on 4.1.2 (which defaults to use C98), and I wonder whether g++ 4.1.2 guarantees that everything happens before (e.g. write to a variable) will be reflected in another thread if:
>> 1. there is no lock (e.g. pthread_mutex/spinlock) involving
>> 2. the "another thread" is created (e.g. calling pthread_create) after the "write"
>
> The relevant question for the compiler is whether it will issue all
> writes to globally visible memory before calling pthread_create.  The
> answer is: yes, it will.
>
> The rest is up to your library.  I would expect that any pthread library
> would have a memory barrier during pthread_create, in which case you
> should be fine.
>
> Ian

  reply	other threads:[~2012-04-17  3:04 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-13  6:55 Hei Chan
2012-04-14  5:46 ` Ian Lance Taylor
2012-04-16  5:51   ` Hei Chan
2012-04-16 16:34     ` Ian Lance Taylor
2012-04-17  3:04       ` Hei Chan [this message]
2012-04-17  4:31         ` Ian Lance Taylor

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=1334631810.73884.YahooMailNeo@web36508.mail.mud.yahoo.com \
    --to=structurechart@yahoo.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=iant@google.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).