This is the mail archive of the libc-help@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: pthread_create()'s behavor?


On Mon, Apr 16, 2012 at 5:36 AM, Bryan Evenson <bevenson@melinkcorp.com> wrote:

> In my experience, when you start dealing with pthreads "indeterminate" is the best description of events. ?If you have some items that must exist before calling pthread_create or if you are creating multiple threads and they need to be created in a specific order, you need to be doing the checks yourself. ?Also, there is always the possibility that the call to new failed to allocate memory and your map list may be empty.
>
> I'd suggest validating that your map is not empty with a call to m_myMap.empty() before you try to perform any action on the map.

Bryan, you are quite mistaken on almost all of the statements you've
made above, and your last advice: calling m_myMap.empty() is simply
nonsense.

>
> -Bryan
>
> -----Original Message-----
> From: libc-help-owner@sourceware.org [mailto:libc-help-owner@sourceware.org] On Behalf Of Hei Chan
> Sent: Monday, April 16, 2012 1:57 AM
> To: libc-help@sourceware.org
> Subject: pthread_create()'s behavor?
>
> 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);
> ?? }
> }


Hei Chan,

The code you provided above can not possibly compile (even after
fixing the missing semicolon, and missing #includes):

t.cc:10: error: extra qualification ‘A::’ on member ‘A’
t.cc: In static member function ‘static void* A::start(void*)’:
t.cc:8: error: invalid use of member ‘A::m_myMap’ in static member function
t.cc:17: error: from this location

You'll have to provide an actual example showing your problem if you want help.

I also suggest running your code through Valgrind and making sure it
reports no errors.

>
> 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.
>
>
> Cheers,
> Hei



-- 
Paul Pluzhnikov


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]