Loading ...
Sorry, an error occurred while loading the content.

Understanding "recursive mutexes in pthreads"

Expand Messages
  • Karthik Parashar
    Hi, I have a problem with this code.The code snippet is as below. I m trying to understand recursive mutexes. The output of the program is as good as the fast
    Message 1 of 1 , Apr 11, 2006
    • 0 Attachment
      Hi,


      I have a problem with this code.The code snippet is as
      below.
      I'm trying to understand recursive mutexes. The output
      of the program is as good as the fast mutex.

      Somebody, please explain this anamoly and tell me how
      to use the recursive mutexes.

      The examples, i've seen so far have recursive mutexes
      being locked in the same thread(It works for me as
      well). So, this is making me believe that recursive
      mutexes cannot be used across threads to lock on the
      mutex recursively.

      Regards,
      ~Karthik.

      PS: Moderators, excuse me for the wrong post i made
      before this.


      ===========CODE Begin=================
      #include <iostream>
      #include <pthread.h>

      using namespace std;

      pthread_mutex_t mute1;
      pthread_cond_t cond;

      void* threadfunc1(void* pv)
      {
      int rc;
      int num =3;
      while(num)
      {
      printf("printing from a thread1\n");
      rc = pthread_mutex_lock(&mute1);
      printf("acquired lock on mutex by 1 -> %d with
      %d\n",rc,0);
      pthread_yield();
      printf("got signal in 1... \n");
      pthread_mutex_unlock(&mute1);
      num--;
      }
      return NULL;
      }

      void* threadfunc3(void* pv)
      {
      int rc;
      int num =3;
      while(num)
      {
      printf("printing from a thread3\n");
      rc = pthread_mutex_lock(&mute1);
      printf("acquired lock on mutex by 3 -> %d with
      %d\n",rc,0);
      pthread_yield();
      printf("got signal in 3... \n");
      pthread_mutex_unlock(&mute1);
      num--;
      }
      return NULL;
      }

      int main()
      {
      pthread_t tid1,tid2,tid3;
      void *ptr1,*ptr2,*ptr3;

      pthread_mutexattr_t mattr;
      pthread_mutexattr_init(&mattr);
      // pthread_mutexattr_settype( \
      &mattr, \
      PTHREAD_MUTEX_RECURSIVE);
      pthread_mutexattr_settype( \
      &mattr, \
      PTHREAD_MUTEX_NORMAL);

      pthread_mutex_init(&mute1,&mattr);

      printf("done with initialization\n");

      pthread_create(&tid1,NULL,threadfunc1,NULL);
      pthread_create(&tid3,NULL,threadfunc3,NULL);
      printf("done with creating threads\n");

      pthread_join(tid1,&ptr1);
      pthread_join(tid3,&ptr3);

      printf("All threads terminated\n");

      return 0;
      }

      ===========CODE End=================

      __________________________________________________
      Do You Yahoo!?
      Tired of spam? Yahoo! Mail has the best spam protection around
      http://mail.yahoo.com
    Your message has been successfully submitted and would be delivered to recipients shortly.