Mutex problem

D

Dan Pavel

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
 
I

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

Hi,

Dan Pavel said:
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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
D

Dan Pavel

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"+computer_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.OpenExisting("Mutex"+computer_name);

I hope that what I wrote here makes sense.

Thank you,
Dan Pavel
 
G

Guest

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("MyProcess" + Machine2Name);
Mutex machine3Mutex = new Mutex("MyProcess" + Machine3Name);

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

// do some work

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

HTH

Ollie Riches
 
I

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

Hi,


Ollie said:
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
 
D

Dan Pavel

I cannot use the mutex like this:
Mutex machine2Mutex = new Mutex("MyProcess" + Machine2Name);
Mutex machine3Mutex = new Mutex("MyProcess" + 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"+computerName);
and I try to open it like this:
mut = Mutex.OpenExisting("myMutex" + computerName);

Thank you,
Dan
 
P

Peter Duniho

Dan said:
[...]
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 "computerName" string (a Dictionary<string, Mutex> would probably
work well for this).

For example:

class MutexFactory
{
static Dictionary<string, Mutex> _dictInstances = new
Dictionary<string, Mutex>();

static public Mutex MutexFromComputerName(string strComputerName)
{
if (!_dictInstances.ContainsKey(strComputerName))
{
_dictInstances.Add(strComputerName, new Mutex(false,
"MyMutex" + strComputerName);
}

return _dictInstances[strComputerName];
}
}

Then:

Mutex mutex = MutexFactory.MutexFromComputerName("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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top