2010-05-25 09:34:06     mutexes

Document created by Aaronwu Employee on Aug 21, 2013
Version 1Show Document
  • View in full screen mode

2010-05-25 09:34:06     mutexes

Filip Vanalme (BELGIUM)

Message: 89832   

 

Hi,

 

Up till now, I was using mutexes with standard attributes. Now, I would like to use the PTHREAD_MUTEX_ERRORCHECK option when creating mutexes. Part of my mutex init code looks like this :

 

pthread_mutexattr_t attr;

 

pthread_mutexattr_init (&attr);

pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ERRORCHECK_NP);

pthread_mutex_init (&mutex, &attr);

pthread_mutexattr_destroy (&attr);

 

I'm creating several mutex, one after the other.

 

I wonder if this is the correct way to do it, because when creating the third mutex, I get a null pointer exception when calling the pthread_mutexattr_init (&attr) function. My concern is the pthread_mutexattr_settype function. When I remove this function (so just init and destroy), everything seems to work fine. In pthread.h, I noticed that pthread_mutexattr_settype is available only when __USE_UNIX98 is defined. Other thing of concern is that I obviously have to use PTHREAD_MUTEX_ERRORCHECK_NP instead of PTHREAD_MUTEX_ERRORCHECK, because PTHREAD_MUTEX_ERRORCHECK seems to be unknown when compiling.

TranslateQuoteReplyEditDelete

 

 

2010-05-25 10:07:16     Re: mutexes

Filip Vanalme (BELGIUM)

Message: 89833   

 

correction : everthing works fine as long as I don't use attr in the call to pthread_mutex_init. So this causes no exception :

 

pthread_mutexattr_t attr;

 

pthread_mutexattr_init (&attr);

pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_ERRORCHECK_NP);

pthread_mutex_init (&mutex, NULL);

pthread_mutexattr_destroy (&attr);

 

when using &attr instead of NULL, I get an exception.

TranslateQuoteReplyEditDelete

 

 

2010-05-25 14:09:10     Re: mutexes

Mike Frysinger (UNITED STATES)

Message: 89838   

 

that code you show looks fine by itself i believe.  i'm guessing you create each thread in the same way and dont just init/destroy the attr once ?

 

you need to use the _NP define because the code you're using is not part of POSIX.  it is thus Non-Portable and you need to use Non-Portable defines because of it.

QuoteReplyEditDelete

Attachments

    Outcomes