pthread_cond_wait() does not wait

Hello,

what reason could it have that a pthread_cond_wait() does not wait for a
desired condition but proceeds and thus consumes the processor in a loop?
Here is the simple example that just does not want to wait until the
condition is met:

1 pthread_mutex_lock( &mutex );
2 while( status != CONDITION )
3 {
4 pthread_cond_wait( &cond, &mutex );
5 }
6 pthread_mutex_unlock( &mutex );

It seems as if “someone” is permanently doing a pthread_cond_signal( &cond )
with a “wrong” CONDITION, which just isn’t the case. Could it be that QNX
handles the condition/mutex differently when a previous error (e.g. while
destroying the mutex/condition and initializing it afterwards) occurred? The
odd thing is that after a complete re-boot the same process waits (as
desired) in line number 4 until the right CONDITION is met. Restarting leads
to such different behavior described above.

Does anyone have an idea?

Thanks.

Nnamdi

1 pthread_mutex_lock( &mutex );
2 while( status != CONDITION )
3 {
4 pthread_cond_wait( &cond, &mutex );
5 }
6 pthread_mutex_unlock( &mutex );

What is the return value from pthread_cond_wait()? Have you verified that
it is returning EOK? Try doing it like this…

#include <stdio.h>
#include <pthread.h>
#include <errno.h>
#include <string.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

pthread_mutex_lock(&mutex);
while( status != CONDITION ) {
int ret = pthread_cond_wait(&cond,&mutex);
if( ret != EOK ) {
fprintf(stderr,“pthread_cond_wait(): %s\n”, strerror(ret));
}
}
pthread_mutex_unlock(&mutex);

\

Chris McKillop <cdm@qnx.com> “The faster I go, the behinder I get.”
Software Engineer, QSSL – Lewis Carroll –
http://qnx.wox.org/

Nnamdi Kohn <nnamdi.kohn@web.de> wrote:

Hello,

what reason could it have that a pthread_cond_wait() does not wait for a
desired condition but proceeds and thus consumes the processor in a loop?
Here is the simple example that just does not want to wait until the
condition is met:

Did you initialize the condition variable & mutex before using them?

pthread_cond_init() and pthread_mutex_init()?

I’ve used this structure repeatedly and successfully in many programs.

-David


1 pthread_mutex_lock( &mutex );
2 while( status != CONDITION )
3 {
4 pthread_cond_wait( &cond, &mutex );
5 }
6 pthread_mutex_unlock( &mutex );

It seems as if “someone” is permanently doing a pthread_cond_signal( &cond )
with a “wrong” CONDITION, which just isn’t the case. Could it be that QNX
handles the condition/mutex differently when a previous error (e.g. while
destroying the mutex/condition and initializing it afterwards) occurred? The
odd thing is that after a complete re-boot the same process waits (as
desired) in line number 4 until the right CONDITION is met. Restarting leads
to such different behavior described above.

Does anyone have an idea?

Thanks.

Nnamdi


Please follow-up to newsgroup, rather than personal email.
David Gibbs
QNX Training Services
dagibbs@qnx.com

Check the return value of pthread_cond_wait(), is it failed ?
Forgot to init the “cond” or init multiple times will case the call
fail.

-xtang

David Gibbs <dagibbs@qnx.com> wrote in message
news:c80gi1$imd$1@inn.qnx.com

Nnamdi Kohn <> nnamdi.kohn@web.de> > wrote:
Hello,

what reason could it have that a pthread_cond_wait() does not wait for a
desired condition but proceeds and thus consumes the processor in a
loop?
Here is the simple example that just does not want to wait until the
condition is met:

Did you initialize the condition variable & mutex before using them?

pthread_cond_init() and pthread_mutex_init()?

I’ve used this structure repeatedly and successfully in many programs.

-David


1 pthread_mutex_lock( &mutex );
2 while( status != CONDITION )
3 {
4 pthread_cond_wait( &cond, &mutex );
5 }
6 pthread_mutex_unlock( &mutex );

It seems as if “someone” is permanently doing a pthread_cond_signal(
&cond )
with a “wrong” CONDITION, which just isn’t the case. Could it be that
QNX
handles the condition/mutex differently when a previous error (e.g.
while
destroying the mutex/condition and initializing it afterwards) occurred?
The
odd thing is that after a complete re-boot the same process waits (as
desired) in line number 4 until the right CONDITION is met. Restarting
leads
to such different behavior described above.

Does anyone have an idea?

Thanks.

Nnamdi


\

Please follow-up to newsgroup, rather than personal email.
David Gibbs
QNX Training Services
dagibbs@qnx.com

What is the return value from
pthread_cond_wait()? Have
you verified that it is returning EOK?
Try doing it like this…

#include <stdio.h
#include <pthread.h
#include <errno.h
#include <string.h

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

pthread_mutex_lock(&mutex);
while( status != CONDITION ) {
int ret = pthread_cond_wait(&cond,&mutex);
if( ret != EOK ) {
fprintf(stderr,“pthread_cond_wait(): %s\n”, strerror(ret));
}
}
pthread_mutex_unlock(&mutex);

Thanks, Chris, it is not returning [EOK] but “Invalid argument”. In my
application I do a different init for cond and mutex, because they reside in
shared memory (to synchronize different processes). This init is executed in
an other process where cond and mutex are variables in shared memory:


pthread_mutexattr_t mutexattr;
pthread_condattr_t condattr;

01 pthread_mutexattr_init( &mutexattr );
02 pthread_mutexattr_setpshared(
03 &mutexattr,
04 PTHREAD_PROCESS_SHARED );
05 pthread_mutex_init( &mutex, &mutexattr );
06 pthread_condattr_init( &condattr );
07 pthread_condattr_setpshared(
08 &condattr,
09 PTHREAD_PROCESS_SHARED );
10 pthread_cond_init( &cond, &condattr );

When ending the application (after ensuring that no other process waits on
the condition anymore nor has the mutex locked) the mutex/condition is
destroyed by:


01 pthread_mutex_destroy( &mutex );
02 pthread_cond_destroy( &cond );

After destroying the mutex/condition, it is initialized by the above code,
used and destroyed again. This second and following init does not do the job
anymore, it seems.

Is there any mistake in initializing or destroying the mutex/condition?

Thanks

Namdi

I have the same problem (Invalid argument) after cond_wait.
I have two processes that pretend use IPC whith mutex & condvars
option.
The server pocess initialize the mutex and condvar on this manner:

AcqShm::Header * hdr =
(AcqShm::Header *) m_shmptr;
rta = pthread_mutexattr_init( & m_mtx_attr );
if(rta != EOK)
{
std::cerr <<
“pthread_mutexattr_setpshared() failed: "
<< rta
<<” errno: "
<<errno<<std::endl;
return false;
}
rta = pthread_mutexattr_setpshared( & m_mtx_attr,
PTHREAD_PROCESS_SHARED );
if(rta != EOK)
{
std::cerr <<
“pthread_mutexattr_setpshared() failed: "
<< rta
<<” errno: "
<<errno<<std::endl;
return false;
}
rta = pthread_mutex_init( & hdr->m_mutex ,
&m_mtx_attr );
if(rta != EOK)
{
std::cerr << “pthread_mutex_init()
failed: " << rta
<<” errno: "
<<errno<<std::endl;
return false;
}
//shareo las condvars
rta = pthread_condattr_init( & m_pop_condattr );
if(rta != EOK)
{
std::cerr <<
“pthread_condattr_init(pop) failed: " <<
rta
<<” errno: "
<<errno<<std::endl;
return false;
}
rta = pthread_condattr_setpshared( &
m_pop_condattr,PTHREAD_PROCESS_SHARED);
if(rta != EOK)
{
std::cerr <<
“pthread_condattr_setpshared(pop) failed: "
<< rta
<<” errno: "
<<errno<<std::endl;
return false;
}
rta = pthread_cond_init( & hdr->m_pop_condvar ,
& m_pop_condattr );
if(rta != EOK)
{
std::cerr <<
"pthread_cond_init(pop) failed: " << rta

<<" errno: "
<<errno<<std::endl;
return false;
}

This work fine. But when I want to wait on the other process (process
client side) with this code:
(m_pHeader is the same shared memory object called hdr in server).

[code:1:abdae1f9fc]resp=pthread_mutex_lock(
&(m_pHeader->m_mutex) );
if(resp != EOK )
{
std::cout <<": mutex lock failed
resp:"<<resp
<<" errno: "
<<errno<<std::endl;
return false;
}
do
{

int resp=pthread_cond_wait(
&(m_pHeader->m_pop_condvar),
&(m_pHeader->m_mutex)
);
if(resp != EOK )
{
std::cout <<": cond wait failed
resp:"<<resp
<<" errno: "
<<errno<<std::endl;
return false;
}
} while( m_pHeader->m_pop_condition ==
k_POP_EMPTY_QUEUE );
process_some_data();
pthread_mutex_unlock( &(m_pHeader->m_mutex)
);[/code:1:abdae1f9fc]

Here I obtain response 22, Invalid argument at pthread_cond_wait()
what is wrong? the mutex? the condvar? both of them?
Thank you in advance,
Matías.