Operating System - HP-UX
1753776 Members
7282 Online
108799 Solutions
New Discussion юеВ

pthread_mutex_init failing on HPUX 11i

 
SOLVED
Go to solution
siba
Advisor

pthread_mutex_init failing on HPUX 11i

Hi,
I am using HPUX 11i(32-bit) with gcc version 3.4.2.

I have a program which forks processes.
The Mutex initialization/deletion is done in the parent process and the locking/Unlocking is done in the child process.

int nReturn = pthread_mutexattr_init(&attrMtx);

nreturn = pthread_mutex_init(SearchLock1, &attrMtx );

nReturn = pthread_mutexattr_destroy(&attrMtx);

pthread_mutex_init is returning EINVAL during Init.

I even tried putting the whole block inside pthread_once() but it still fails.

Any clue!!!!!
4 REPLIES 4
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: pthread_mutex_init failing on HPUX 11i

I can't see your call to pthread_mutex_init but EINVAL is returned if either parameter is invalid is some way. If your are really saying that pthread_mutexattr_init() is returning EINVAL then the answer is the same --- a bad parameter. For this function, I would send in a NULL pointer (properly cast) so that the defaults would be used and see if that clears your problem. You could then set about properly initializing your values before sending them in.
If it ain't broke, I can fix that.
siba
Advisor

Re: pthread_mutex_init failing on HPUX 11i

hi stephens,
Thanks for the clue. That works now but i am still struck :)

i have a small code that spawns child processes and each of the child process increments a global var.

int retval = pthread_mutex_lock(&SearchLockspd11) ;
for(int i=0; i < 10; i++)
{
g_GlobaInt++;
sleep(1);
}
retval= pthread_mutex_unlock(&SearchLockspd11) ;


Although the return value of Lock and Unlock are 0, the Lock is not happening.
I mean as per the code above it should print all 10 values for g_GlobaInt for process1 and then should go ahead with process2. But currently i am able to see a mix of both the processes in the logs. Is there something wrong which i am doing???
siba
Advisor

Re: pthread_mutex_init failing on HPUX 11i

Here goes my full sample code...

#include
#include
#include
#include

#include
#include

typedef pthread_mutex_t *LPCRITICAL_SECTION;
typedef pthread_mutex_t CRITICAL_SECTION;
int g_Val=0;
int ThreadID =0;
int ThreadIDChild =0;

static pthread_mutex_t SearchLockspd11; // = PTHREAD_MUTEX_INITIALIZER;
static pthread_once_t once_control = PTHREAD_ONCE_INIT;
static pthread_once_t once_control1 = PTHREAD_ONCE_INIT;


void initialize123()
{
int nreturn = 0;
pthread_mutexattr_t attrMtx;
int nReturn = pthread_mutexattr_init(&attrMtx);
nreturn = pthread_mutex_init(&SearchLockspd11, &attrMtx );
nReturn = pthread_mutexattr_destroy(&attrMtx);

}
void initialize1()
{
pthread_mutex_destroy(&SearchLockspd11);

}


main()
{
printf("ENOSYS = %d\n", ENOSYS);

int i = 0;
int j = 0;
int nTemp=0;
pthread_once(&once_control, initialize123);
ThreadID = getpid();

printf("Original....PID is %d, PPID is %d\n", getpid(), getppid());
for(i=0; i < 50; i++)
{
if(getpid() == ThreadID)
{
fork();
if(getppid() == ThreadID)
{
nTemp = pthread_mutex_lock(&SearchLockspd11);
for(j=0; j<5;j++)
incrementCount();
nTemp = pthread_mutex_unlock(&SearchLockspd11);
exit(0);
}
else
{
if(getppid() == ThreadID)
{
exit(0);
i--;
}
}
}
else
{
exit(0);
i--;
}
}
pthread_once(&once_control1, initialize1);

exit(0);
return;

}

int incrementCount()
{
int nTemp=0;
g_Val++;
sleep(1);
}
~
siba
Advisor

Re: pthread_mutex_init failing on HPUX 11i

Invalid post