using mutexes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • iceman

    using mutexes

    Hi All,

    Suppose that my program is such that


    int counter;
    pthread_mutex_t mutex1 = PTHREAD_MUTEX_I NITIALIZER;
    void *functionC()
    {
    pthread_mutex_l ock( &mutex1 );
    counter++;
    printf("Counter value: %d\n",counter);
    pthread_mutex_u nlock( &mutex1 );
    }

    int main()
    {
    pthread_t thread_id;
    pthread_create (&thread_id, NULL, &functionC, NULL);
    //increment counter in main also

    }


    Will there be a problem when I do this?If the thread is schduled first
    will the main() wait untill the thread unlocks?
    If not,Is there anyway to lock the variable in the main program as
    well as in the thread.

    Cheers

  • Martin York

    #2
    Re: using mutexes

    Will there be a problem when I do this?
    No problems.



    If the thread is schduled first
    will the main() wait untill the thread unlocks?
    No. To make the main thread wait you must make it try and take the
    lock.

    If not,Is there anyway to lock the variable in the main program as
    well as in the thread.
    Yes:
    // Init counter.
    int counter = 0;


    // I hope this is some strange way of calling
    // pthread_mutex_i nit()
    pthread_mutex_t mutex1 = PTHREAD_MUTEX_I NITIALIZER;


    void *functionC(void *)
    {
    inc_counter();
    }

    void inc_counter()
    {
    pthread_mutex_l ock( &mutex1 );
    counter++;
    printf("Counter value: %d\n",counter);
    pthread_mutex_u nlock( &mutex1 );

    }

    int main()
    {
    pthread_t thread_id;
    pthread_create (&thread_id, NULL, &functionC, NULL);

    //increment counter in main also
    inc_counter();
    }


    Comment

    • Ian Collins

      #3
      Re: using mutexes

      iceman wrote:
      Hi All,
      >
      Suppose that my program is such that
      >
      >
      int counter;
      pthread_mutex_t mutex1 = PTHREAD_MUTEX_I NITIALIZER;
      May I suggest you ask on comp.programmin g.threads?

      pthreads are topical there, but not here.

      --
      Ian Collins.

      Comment

      • Szabolcs Ferenczi

        #4
        Re: using mutexes

        iceman wrote:
        Will there be a problem when I do this?If the thread is schduled first
        will the main() wait untill the thread unlocks?
        The main thread will not wait but you can arrange that it does. If you
        insert a pthread join operation, your main thread will wait until the
        forked thread finishes.

        int main()
        {
        pthread_t thread_id;
        pthread_create (&thread_id, NULL, &functionC, NULL);
        //increment counter in main also
        pthread_join(th read_id, NULL);
        }

        Best Regards,
        Szabolcs

        Comment

        • James Kanze

          #5
          Re: using mutexes

          On Feb 12, 6:07 am, iceman <jegan...@gmail .comwrote:
          Suppose that my program is such that
          int counter;
          pthread_mutex_t mutex1 = PTHREAD_MUTEX_I NITIALIZER;
          void *functionC()
          {
          pthread_mutex_l ock( &mutex1 );
          counter++;
          printf("Counter value: %d\n",counter);
          pthread_mutex_u nlock( &mutex1 );
          }
          int main()
          {
          pthread_t thread_id;
          pthread_create (&thread_id, NULL, &functionC, NULL);
          //increment counter in main also
          }
          That looks more like Posix C than anything C++. You really
          should ask in a Posix (Unix) group, rather than here.
          Will there be a problem when I do this?
          Not if you synchronize the access to counter in main as well.
          The Posix rule (which, I believe, will be the C++ rule when C++
          adds threading in the next version) is simple: if any thread
          modifies an object, and more than one thread accesses the
          object, then all accesses must be externally synchronized.
          If the thread is schduled first
          will the main() wait untill the thread unlocks?
          Why should it?
          If not,Is there anyway to lock the variable in the main
          program as well as in the thread.
          The same way you locked it in the thread.

          --
          James Kanze (GABI Software) email:james.kan ze@gmail.com
          Conseils en informatique orientée objet/
          Beratung in objektorientier ter Datenverarbeitu ng
          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

          Comment

          Working...