* Re: mutex problem using DECthreads
2001-08-13 6:53 mutex problem using DECthreads Raj Calisa
@ 2001-08-13 11:19 ` David Butenhof
2001-08-13 11:24 ` McNepp
1 sibling, 0 replies; 3+ messages in thread
From: David Butenhof @ 2001-08-13 11:19 UTC (permalink / raw)
Raj Calisa wrote:
> I am using DECthreads on Digital Unix 4.0F (patch kit 5).
Although this should be completely irrelevant, you should consider
installing patch kit 6, which has been out since February.
> 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?
You cannot statically initialize a dynamically allocated mutex, even where
the language would allow the syntax. For example, a local variable in a
procedure cannot be statically initialized with PTHREAD_MUTEX_INITIALIZER,
even though the compiler will happily apply the syntax.
It's fine, however, to dynamically initialize any mutex, no matter where
it's been allocated... as long as it hasn't previously been initialized (or
has been destroyed since an earlier initialization).
> If so, does a mutex wrapped into a class whose object gets created
> statically qualify as a static mutex?
If the storage for the mutex is allocated by the compiler, rather than at
runtime, then it's "static". In C, that covers variables declared
with the keyword "static", or (explicitly or implicitly) with the keyword
"extern". Anything else is dynamic -- stack allocations via local variable
declarations, heap (new) allocations, etc.
The C++ "static" is similar, though not quite the same. Still, even in a
"static" object, you may have pointers to dynamically allocated objects.
You don't say enough about your program's structure to answer your question
with any certainty.
> I checked the documentation 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.
A statically initialized mutex (PTHREAD_MUTEX_INITIALIZER) will be
"completed" the first time it's used by the program. By the way, the
implementation commentary in pthread.h is there to be nice, but,
technically, it's not formal "documentation". Nothing you learn from it
should ever be used in a program, because most of it can change without
notice at any time.
> 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.
"Presumably" static? What does THAT mean? Either it's static or it's not;
if this is your program you can tell by reading the source.
> Is DECthreads doing something consciously to reject the call to
> pthread_mutex_init()? I am not ruling out memory corruption, but
> I would like to explore other possibilities before delving into
> that one.
Are you checking for pthread_mutex_init() failure? What do you DO if it
fails? For that matter, how is pthread_mutex_lock() failing?
Finally... while you can explore other possibilities as much as you'd like,
your problem is most likely to be memory corruption. Running with Visual
Threads may help you find a problem, if its caused by improper
synchronization. You can get more info, and the latest kit, at
http://www.compaq.com/products/software/visualthreads/.
/------------------[ David.Butenhof@compaq.com ]------------------\
| Compaq Computer Corporation POSIX Thread Architect |
| My book: http://www.awl.com/cseng/titles/0-201-63392-2/ |
\-----[ http://home.earthlink.net/~anneart/family/dave.html ]-----/
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: mutex problem using DECthreads
2001-08-13 6:53 mutex problem using DECthreads Raj Calisa
2001-08-13 11:19 ` David Butenhof
@ 2001-08-13 11:24 ` McNepp
1 sibling, 0 replies; 3+ messages in thread
From: McNepp @ 2001-08-13 11:24 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 3+ messages in thread