From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fa528,bc5b0be4bdad749d X-Google-Attributes: gidfa528,public X-Google-Thread: ffc1e,bc5b0be4bdad749d X-Google-Attributes: gidffc1e,public X-Google-ArrivalTime: 2001-08-13 04:30:00 PST Path: archiver1.google.com!news2.google.com!postnews1.google.com!not-for-mail From: gn@langner.com (McNepp) Newsgroups: comp.programming.threads,comp.unix.tru64 Subject: Re: mutex problem using DECthreads Date: 13 Aug 2001 04:24:17 -0700 Organization: http://groups.google.com/ Message-ID: <97e61727.0108130324.6efbab47@posting.google.com> References: <3B77796D.6218EA20@ot.com.au> NNTP-Posting-Host: 62.157.192.1 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 997701857 23030 127.0.0.1 (13 Aug 2001 11:24:17 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 13 Aug 2001 11:24:17 GMT Xref: archiver1.google.com comp.programming.threads:5400 comp.unix.tru64:4166 Raj Calisa wrote in message news:<3B77796D.6218EA20@ot.com.au>... > Hi all, > > I am using DECthreads on Digital Unix 4.0F (patch kit 5). > > In our, application, one of our threads cause the application > to crash due to an assertion failure. This happens when it tries > to acquire a mutex and it fails. This mutex happens to be a static > mutex. It is wrapped up in a C++ class which is created statically. > The mutex itself is created in the constructor of the class using > pthread_mutex_init() rather than PTHREAD_MUTEX_INITIALIZER. I > remember reading somewhere that all static mutexes must be initialized > using the macro and non-static ones with the init() function, otherwise > one would get undefined behaviour. > Is this true? Not entirely. You may well declare an object of type "pthread_mutex_t" with static storage duration and _NOT_ initialize it with PTHREAD_MUTEX_INITIALIZER. It will then be zero-initialized by the compiler. If you ensure that you initialize it with a call to pthread_mutex_init() prior to its first use, there shouldn't be a problem! > If so, does a mutex wrapped into a class whose object gets created > statically qualify as a static mutex? > Not entirely. The mutex is a member of the enclosing class which has a constructor and private members (one of which is obviously the mutex). Thus, the class object cannot be statically initialized with "{}"-expressions, effectively ruling out the use of PTHREAD_MUTEX_INITIALIZER. You need to initialize the mutex with a call to pthread_mutex_init, preferredly in the contstructor of the enclosing class. So far, it looks as though you had done everything correctly! > I checked the documen tation in pthread.h and it says that the "sequence" > field of the mutex object is set to 0 initially for static mutexes > initialized with the macro and to some non-zero value for the one > created using the init() call. > > The mutex in question is presumably static, but initialized with > the init() call and has a sequence number of "0" when I checked it > under the debugger. The "lock" field wass having a bad value as well, > which explains why mutex_lock() failed. > Hmm, this looks as if the constructor of the static object has not been called. In theory, this can only happen if the thread that tries to access the mutex is created before main() is entered. Could that be the case? Maybe you are creating a thread from the constructor of another class-object with static storage duration?! HTH, Gernot