Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: static object of mutex class is not getting initialized



On Dec 19, 2005, at 6:44 AM, Amol Gajewar wrote:
Static and global object of one class (mutex ) is not getting initialized before it is used. Its default constructor is not called,
On the other hand if i make it local it is working properly.
Why this is happening ?

The pthread mutexes are not class objects, and therefore do not have default constructors, regardless of which language you are using them from.


You must explicitly assign the mutex the value PTHREAD_MUTEX_INITIALIZER, or you must explicitly call pthread_mutex_init() on it before using it. The PTHREAD_MUTEX_INITIALIZER was added to draft 5 of the standard, and survived into draft 10, which became the final version.

See also <http://www.opengroup.org/onlinepubs/009695399/functions/pthread_mutex_init.html >

And here's a threads programming guide, if you won't/can't get the book I suggested the other day:

	<http://www.llnl.gov/computing/tutorials/pthreads/>

And here's the specific information on programming mutexes:

	<http://www.llnl.gov/computing/tutorials/pthreads/#Mutexes>

I still recommend the O'Reilly book, however:

	<http://www.oreilly.com/catalog/pthread/index.html>

Many systems are actually at draft 4 of the standard, and don't have the value PTHREAD_MUTEX_INITIALIZER; if you are porting the code from a draft 4 standard system, you will need to change mutex initialization, and you will also need to change the arguments to pthread_create (which used to take an address of a pointer argument, but now takes a pointer argument).

Draft 4 systems include HPUX before version 11, OSF DCE, and SGI's IRIX.

The normal general way this is handled at compile time is actually to use PTHREAD_MUTEX_INITIALIZER as a guard macro, e.g.:


#ifdef PTHREAD_MUTEX_INITIALIZER /* draft 5 or later; more likely, a standard pthreads system */ ... #else /* draft 4 or older non-standard pthreads system */ ... #endif

The OpenLDAP project tends to get this right (so does the MCSC C++ STL, and so does MySQL - not surprising, since I did the original patches for all three of them).

The O'Reilly book dedicates a chapter to the differences ("Appendix B: Pthreads Draft vs. the Final Standard").

Without seeing your code, I can't answer why the local variable is working for you, since if you aren't explicitly initializing it, either, then you'd most likely be getting stack garbage, and you'd only have a 2 in 4 billion chance of it being exactly the right stack garbage for it to work. So I'm guessing you initialize it.

-- Terry
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Unix-porting mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/unix-porting/email@hidden

This email sent to email@hidden
References: 
 >static object of mutex class is not getting initialized (From: "Amol Gajewar" <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.