how to use mutex

Chan Kim ckim at etri.re.kr
Wed Sep 2 01:26:14 UTC 2015


Hello, rtems users,

Please somebody help with with using mutex.
To avoid sync problem by simpling serializing read and write processing, I tried using mutex and found it's not working properly.

------------ the way I did it 
pthread_mutexattr_t  mutex_attr;
pthread_mutex_t rd_wr_mutex;

to initialize,
    statusi = pthread_mutexattr_init( &mutex_attr );
    printk("mutext attri init result = %x\n", statusi);
    pthread_mutex_init(&rd_wr_mutex, &mutex_attr);
    printk("mutext init result = %x\n", statusi);

to lock,
    pthread_mutex_lock(&rd_wr_mutex); // rtems_mutex_t
    printk("locked from read..\n");

to unlock,
    printk("unlocking from read..\n");
    pthread_mutex_unlock(&rd_wr_mutex);

--------------
I see results (only mutex lock, unlock prints)

mutext attr init result = 0
mutext init result = 0
...
locked from read..
unlocking from read..
locked from write..
unlocking from write..
locked from write..
unlocking from write..
locked from write..
unlocking from write..
locked from read..
locked from write..    <===== ???


Print result shows teh write thread enters it when read thread has locked it. What's wrong with the way of my using mutex?
Hope someone clears it with some explanation.
Thanks,

regards,
Chan Kim


More information about the users mailing list