mutex example

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • friend.blah@googlemail.com

    mutex example

    can any one give a simple example for implementing mutex.
    I have 3 threads (taking different input files )enter into the same
    function at different times...

    The 3 threads need to exit/leave the function till all the input files
    reached till end which are taken as input to the threds.


    thanks to all

  • Victor Bazarov

    #2
    Re: mutex example

    friend.blah@goo glemail.com wrote:
    can any one give a simple example for implementing mutex.
    I have 3 threads (taking different input files )enter into the same
    function at different times...
    >
    The 3 threads need to exit/leave the function till all the input files
    reached till end which are taken as input to the threds.
    At this point "mutex" or any other synchronisation object are platform-
    specific. Consider asking your question in the newsgroup for your platform.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask

    Comment

    • Pascal J. Bourguignon

      #3
      Re: mutex example

      "friend.blah@go oglemail.com" <friend.blah@go oglemail.comwri tes:
      can any one give a simple example for implementing mutex.
      I have 3 threads (taking different input files )enter into the same
      function at different times...
      >
      The 3 threads need to exit/leave the function till all the input files
      reached till end which are taken as input to the threds.

      So you want the three threads in the same function at the same time.
      So you want absolutely no mutual exclusion, but exactly the opposite,
      you want mutual INCLUSION!

      You should try to express what you want more clearly. For example,
      you could start with something more grammatically correct. I have
      really a hard time parsing this sentence:

      "The 3 threads need to exit/leave the function till all the input
      files reached till end which are taken as input to the threds."


      Let's assume what you want is:


      thread 1
      |
      |
      v
      ----- enter function
      |
      |
      v
      (go on reading file 1)
      | thread 2
      | |
      | |
      | v
      | ----- enter function
      | |
      | |
      | v
      | (go on reading file 2)
      | |
      | |
      | |
      | | thread 3
      | | |
      | | |
      | | v
      | | ----- enter function
      | | |
      | | |
      | | v
      | | (go on reading file 3)
      v | |
      ----- reach eof v |
      | ----- reach eof |
      | | v
      | | ----- reach eof
      | | |
      v v v
      ---------------------------------------------------- exit function
      | | |
      | | |
      v v v


      The synchronization at the exit function could be implemented with a
      condition.


      initialize:
      - a counter = 0
      - a mutex m
      - a condition c



      synchronized_ex it(n)
      mutext_lock(m)
      increment counter
      if (counter >= n) then
      condition_broad cast(c)
      counter=0
      else
      condition_wait( c,m)
      endif
      mutext_unlock(m )


      All three threads shall call synchronized_ex it(3) after reaching their EOF.


      man pthread_cond_in it pthread_cond_wa it pthread_cond_br oadcast
      man pthread_mutex_i nit pthread_mutex_l ock pthread_mutex_u nlock


      --
      __Pascal Bourguignon__

      Comment

      Working...