Mutex problem

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

    #1

    Mutex problem



    Hi,

    I did not used Mutex before and I need now.
    I have an application that control some workflows on different machines.
    I use SNMP for that.
    The problem is that the thread used for starting an workflow need to
    write in some OID. When I start 2 workflows on the same machine the
    second thread need to wait for the first one to finish and after that
    continue work. This works OK. The problem is when I have to start 2
    workflows on different machines on the same time, the second still wait
    for the first one to finish. Somebody told me to use named mutex but I
    am lost now.
    Please give me some ideeas.

    Best regards,
    Dan Pavel

    *** Sent via Developersdex http://www.developersdex.com ***
  • Ignacio Machin \( .NET/ C# MVP \)

    #2
    Re: Mutex problem

    Hi,

    "Dan Pavel" <myalternativma il@yahoo.comwro te in message
    news:%23C%23B%2 38GyHHA.3940@TK 2MSFTNGP05.phx. gbl...
    >
    >
    Hi,
    >
    I did not used Mutex before and I need now.
    I have an application that control some workflows on different machines.
    I use SNMP for that.
    The problem is that the thread used for starting an workflow need to
    write in some OID. When I start 2 workflows on the same machine the
    second thread need to wait for the first one to finish and after that
    continue work. This works OK.
    How this work ok?
    The problem is when I have to start 2
    workflows on different machines on the same time, the second still wait
    for the first one to finish. Somebody told me to use named mutex but I
    am lost now.
    So I'm , a Mutex work only in the local machine.


    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Mutex problem

      Dan,

      It sounds like you need to synchronize these processes between the two
      machines. If that is the case, then a Mutex will not work, since the Mutex
      will only work on the one machine. You will have to set up a service that
      the two processes access which will let them know when to perform their
      work.

      You might want to think of dividing the work of that one process into
      two separate processes, and then having the second process trigger the
      second part of the first process.


      --
      - Nicholas Paldino [.NET/C# MVP]
      - mvp@spam.guard. caspershouse.co m

      "Dan Pavel" <myalternativma il@yahoo.comwro te in message
      news:%23C%23B%2 38GyHHA.3940@TK 2MSFTNGP05.phx. gbl...
      >
      >
      Hi,
      >
      I did not used Mutex before and I need now.
      I have an application that control some workflows on different machines.
      I use SNMP for that.
      The problem is that the thread used for starting an workflow need to
      write in some OID. When I start 2 workflows on the same machine the
      second thread need to wait for the first one to finish and after that
      continue work. This works OK. The problem is when I have to start 2
      workflows on different machines on the same time, the second still wait
      for the first one to finish. Somebody told me to use named mutex but I
      am lost now.
      Please give me some ideeas.
      >
      Best regards,
      Dan Pavel
      >
      *** Sent via Developersdex http://www.developersdex.com ***

      Comment

      • Dan Pavel

        #4
        Re: Mutex problem


        Sorry for my lame exprimation, I am not too good to English.

        My app is running on machine1. The app sends through SNMP instructions
        to a service on machine2 and machine3.
        Everything is happening on machine1.
        I have a Timer that periodically verify that scheduled start time is Now
        and if so start a thread. This thread is sending instructions to that
        machine that is scheduled to start a workflow.
        The problem is when I try to start 2 workflows on the same time. There
        are 2 cases:

        Case 1: The 2 workflows are scheduled to start on the same machine
        (machine2) on the same time. There are 2 threads started, one for each
        workflow. I use a Mutex for that because I cannot write in the same
        memory place starting instructions in the same time. I need to do it
        sepparately. Start first workflow and then send instructions to start
        the second workflow. The first thread start and get the ownership of the
        mutex and continue work. The second thread wait until the mutex is
        released by the first thread and continue the work. This is already done
        and works OK.

        Case 2: The 2 workflows are scheduled to start on the same time on
        different machines (workflow1 on machine 2 and worflow2 on machine3).
        Using the code described above these 2 threads will not go on the same
        time. Thread2 will wait for thread1 to finish. This is not OK. I need
        them (threads) to work in the same time. In this case is ok to write in
        that particular shared memory area because they are on sepparate
        machines. So for every machine I try to create a Mutex with name:
        mut = new Mutex(false, "Mutex"+compute r_name);
        but I don't know how can I use it or how can I find that a mutex is for
        a particular machine.
        I found this but i don't know how reliable is:
        mut = Mutex.OpenExist ing("Mutex"+com puter_name);

        I hope that what I wrote here makes sense.

        Thank you,
        Dan Pavel

        *** Sent via Developersdex http://www.developersdex.com ***

        Comment

        • =?Utf-8?B?T2xsaWU=?=

          #5
          RE: Mutex problem

          As I understand your problem a mutex will work for your situation, machine 1
          is the controller and allocates work to machine 2 or machine 3, if 2 pieces
          of work are to run at the same time on the same machine only 1 piece of work
          should be processed at a time (the processing of the work is in a 'serial'
          manner), but if they are to be processed on seperate machines then the can be
          processed at the same time (the processing of the work is in 'parallel'
          manner)

          Create an instance of the Mutex object on each thread and then attempt to
          use that mutext on the thread, if another thread already has the Mutext
          object it will block until it becomes available OR a timeout value is reached.

          Mutex machine2Mutex = new Mutex("MyProces s" + Machine2Name);
          Mutex machine3Mutex = new Mutex("MyProces s" + Machine3Name);

          // this will wait indefinitely to obtain the Mutex
          machine2Mutex.W aitOne();

          // do some work

          // release the mutex so that any other thread\process can obtain the mutex
          machine2Mutex.R eleaseMutex();

          HTH

          Ollie Riches






          "Dan Pavel" wrote:
          >
          >
          Hi,
          >
          I did not used Mutex before and I need now.
          I have an application that control some workflows on different machines.
          I use SNMP for that.
          The problem is that the thread used for starting an workflow need to
          write in some OID. When I start 2 workflows on the same machine the
          second thread need to wait for the first one to finish and after that
          continue work. This works OK. The problem is when I have to start 2
          workflows on different machines on the same time, the second still wait
          for the first one to finish. Somebody told me to use named mutex but I
          am lost now.
          Please give me some ideeas.
          >
          Best regards,
          Dan Pavel
          >
          *** Sent via Developersdex http://www.developersdex.com ***
          >

          Comment

          • Ignacio Machin \( .NET/ C# MVP \)

            #6
            Re: Mutex problem

            Hi,


            "Ollie" <ollie_riches@h otmail.comwrote in message
            news:423C1D48-BCB6-46E4-876E-42AA1CD4BD6D@mi crosoft.com...
            As I understand your problem a mutex will work for your situation, machine
            1
            is the controller and allocates work to machine 2 or machine 3, if 2
            pieces
            of work are to run at the same time on the same machine only 1 piece of
            work
            should be processed at a time (the processing of the work is in a 'serial'
            manner), but if they are to be processed on seperate machines then the can
            be
            processed at the same time (the processing of the work is in 'parallel'
            manner)

            Good guess, I was unable to got it from the original post


            Comment

            • Dan Pavel

              #7
              Re: Mutex problem

              I cannot use the mutex like this:
              Mutex machine2Mutex = new Mutex("MyProces s" + Machine2Name);
              Mutex machine3Mutex = new Mutex("MyProces s" + Machine3Name);

              because I don't know how many machines the client have.

              What I need to do is to create a class of type map where to store the
              created mutex. When I try to use a mutex, I search there and if exists,
              I try to open or wait, or if it's not exists I create a new one and
              store it there.
              My question is how can I create this class, what's the best approach ?

              I try to create the mutex like this:
              mut = new Mutex(false, "MyMutex"+compu terName);
              and I try to open it like this:
              mut = Mutex.OpenExist ing("myMutex" + computerName);

              Thank you,
              Dan

              *** Sent via Developersdex http://www.developersdex.com ***

              Comment

              • Peter Duniho

                #8
                Re: Mutex problem

                Dan Pavel wrote:
                [...]
                What I need to do is to create a class of type map where to store the
                created mutex. When I try to use a mutex, I search there and if exists,
                I try to open or wait, or if it's not exists I create a new one and
                store it there.
                My question is how can I create this class, what's the best approach ?
                Sounds like you want a class factory where instances are stored indexed
                by the "computerNa me" string (a Dictionary<stri ng, Mutexwould probably
                work well for this).

                For example:

                class MutexFactory
                {
                static Dictionary<stri ng, Mutex_dictInsta nces = new
                Dictionary<stri ng, Mutex>();

                static public Mutex MutexFromComput erName(string strComputerName )
                {
                if (!_dictInstance s.ContainsKey(s trComputerName) )
                {
                _dictInstances. Add(strComputer Name, new Mutex(false,
                "MyMutex" + strComputerName );
                }

                return _dictInstances[strComputerName];
                }
                }

                Then:

                Mutex mutex = MutexFactory.Mu texFromComputer Name("my computer name");

                You'll note that the above only creates new mutexes. I'm assuming that
                what you really needed was the example of the factory and that you will
                be able to easily modify it to deal with attempting to open an existing
                mutex first before creating one, per your own requirements.

                Pete

                Comment

                • Dan Pavel

                  #9
                  Re: Mutex problem



                  It worked. Thank you. :)

                  Dan

                  *** Sent via Developersdex http://www.developersdex.com ***

                  Comment

                  Working...