mutex, condvar and ShMem

Hello,

I use shared memory to be able to use mutexes and condvars between different
processes. As in the example in Krten’s book I do the following steps:

  1. initialize a local variable mutex_init and condvar_init with
    PTHREAD_MUTEX_INITIALIZER and PTHREAD_COND_INITIALIZER macros.

  2. copy the contents of these local variables to global variables the shared
    memory region “memcpy(&mutex_var_in_shmem,&local_mutex_var,
    sizeof(pthread_mutex_t))” (the same with condvar).

  3. enter endless loop where I wait for a certain condition (according to the
    example in the book)

now running other processes (first they register for the same shared
memory…) leads to the situation that the conditions work fine but the
computer is very very slow even if nothing is happening and all connected
processes are waiting for the next condition. When I quit all processes and
restart them the same situation appears.

What could the reason be for the slowing down from the second process on?

Nnamdi

Nnamdi Kohn <nnamdi.kohn@tu-bs.de> wrote:

I use shared memory to be able to use mutexes and condvars between different
processes. As in the example in Krten’s book I do the following steps:

  1. initialize a local variable mutex_init and condvar_init with
    PTHREAD_MUTEX_INITIALIZER and PTHREAD_COND_INITIALIZER macros.

  2. copy the contents of these local variables to global variables the shared
    memory region “memcpy(&mutex_var_in_shmem,&local_mutex_var,
    sizeof(pthread_mutex_t))” (the same with condvar).

This creates a mutex with default attributes, and the default value of
the process-shared attribute is PTHREAD_PROCESS_PRIVATE. Take a look at
pthread_mutexattr_setpshared() in the docs.

Wojtek Lerch <wojtek_l@yahoo.ca> wrote:

Nnamdi Kohn <> nnamdi.kohn@tu-bs.de> > wrote:
I use shared memory to be able to use mutexes and condvars between different
processes. As in the example in Krten’s book I do the following steps:

  1. initialize a local variable mutex_init and condvar_init with
    PTHREAD_MUTEX_INITIALIZER and PTHREAD_COND_INITIALIZER macros.

  2. copy the contents of these local variables to global variables the shared
    memory region “memcpy(&mutex_var_in_shmem,&local_mutex_var,
    sizeof(pthread_mutex_t))” (the same with condvar).

This creates a mutex with default attributes, and the default value of
the process-shared attribute is PTHREAD_PROCESS_PRIVATE. Take a look at
pthread_mutexattr_setpshared() in the docs.

Or, in more detail, you will have to explicitly initialize any mutexes and
condvars that will be used in shared memory between processes, as there is
no default initializer that will work.

Start with pthread_mutex_init(), and pthread_cond_init(), and look backwards
to all the pthread_mutexattr*() and pthread_condattr*() functions.

e.g.

pthread_mutexattr_init()
pthread_mutexattr_setpshared()
pthread_mutex_init()

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

Thank you. It works fine now.

Nnamdi

“Wojtek Lerch” <wojtek_l@yahoo.ca> schrieb im Newsbeitrag
news:b6kj82$i2v$1@inn.qnx.com

Nnamdi Kohn <> nnamdi.kohn@tu-bs.de> > wrote:
I use shared memory to be able to use mutexes and condvars between
different
processes. As in the example in Krten’s book I do the following steps:

  1. initialize a local variable mutex_init and condvar_init with
    PTHREAD_MUTEX_INITIALIZER and PTHREAD_COND_INITIALIZER macros.

  2. copy the contents of these local variables to global variables the
    shared
    memory region “memcpy(&mutex_var_in_shmem,&local_mutex_var,
    sizeof(pthread_mutex_t))” (the same with condvar).

This creates a mutex with default attributes, and the default value of
the process-shared attribute is PTHREAD_PROCESS_PRIVATE. Take a look at
pthread_mutexattr_setpshared() in the docs.