This is the mail archive of the
libc-help@sourceware.org
mailing list for the glibc project.
Re: pthread_create()'s behavor?
- From: Ángel González <keisial at gmail dot com>
- To: Hei Chan <structurechart at yahoo dot com>
- Cc: "libc-help at sourceware dot org" <libc-help at sourceware dot org>
- Date: Tue, 17 Apr 2012 16:54:55 +0200
- Subject: Re: pthread_create()'s behavor?
- References: <1334555816.26507.YahooMailNeo@web36501.mail.mud.yahoo.com>
On 16/04/12 07:56, Hei Chan wrote:
> Hi,
>
> I am using pthread library on CentOS 5.5.
>
> I have the following code:
>
> class B {};
>
> class A {
> private:
> std::map<long, B*> m_myMap;
> public:
> A::A() {
> m_myMap[0] = new B();
> pthread_t threadID;
> pthread_create(&threadID, NULL, start, NULL);
> }
> static void* start(void*) {
> // use m_myMap[0] here, will this new thread sees everything happens before it gets created (e.g. m_myMap[0] contains the valid value)?
> m_myMap.find(0);
> }
> }
>
> Sometimes (~1% chance), I got a segfault at m_myMap.find(0). Is it true that pthread_create() won't try to enforce the memory barrier to ensure all the previous writes to be globally visible?
>
> Thanks in advance.
I'm not seeing with your code (after fixes) that behavior here (which
doesn't mean it would never fail for me), but I wonder if that's really
the code that fais for you.
Is m_myMap a static parameter? Are other threads reading/writing to it?
Another thread writing m_MyMap[5] could perfectly produce a segfault on
m_myMap.find(0), and that would be expected.