From: gn@langner.com (McNepp)
Subject: Re: mutex problem using DECthreads
Date: 13 Aug 2001 04:24:17 -0700 [thread overview]
Message-ID: <97e61727.0108130324.6efbab47@posting.google.com> (raw)
In-Reply-To: 3B77796D.6218EA20@ot.com.au
Raj Calisa <rajc@ot.com.au> 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
prev parent reply other threads:[~2001-08-13 11:24 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2001-08-13 6:53 mutex problem using DECthreads Raj Calisa
2001-08-13 11:19 ` David Butenhof
2001-08-13 11:24 ` McNepp [this message]
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox