semaphore and mutex feature joined together, is it possible?

  • Thread starter Thread starter Jun Kusanagi
  • Start date Start date
J

Jun Kusanagi

Is there any way to solve process synchronization between more than 1
resources.
I'm already trying to use Mutex.WaitAny, but I confused to execute
Release method from the corresponding mutexes.
Any comment will be appreciated.

Best Regards,
 
Need more info. What are you trying to do.

--
William Stacey [MVP]

| Is there any way to solve process synchronization between more than 1
| resources.
| I'm already trying to use Mutex.WaitAny, but I confused to execute
| Release method from the corresponding mutexes.
| Any comment will be appreciated.
|
| Best Regards,
|
 
Hi William,
thanks for replying, the requirement is:
1. there's 2 resources (R1 & R2), which need to be accessed in a serial
mode
2. access to this resources is coming from 3 different multithreaded
process (P1, P2, P3).
I try to solve this requirement by using mutexes,
P1 using the R1, it locked this resources, so R1 can't be used until P1
is releasing R1.
The question is: by using Mutex.WaitAny for queueing P1, P2, P3
accessing R1 & R2, I can't find out whether R1 or R2 is used, so I
don't know which Mutex to be released. If this kind of requirement
can't be solved by using mutexes, any other brief solution is highly
appreciated.

Best Regards,
 
Are P1,2,3 actually processes or threads in the same app?

Here is how you might approach it using one mutex to protext both R1 and R2.
// Create Mutex in first P.

Mutex m = new Mutex(false, "mymutex");



// P1,2,3 will get existing mutex and wait on it (timeout recommended), do
work, and release it.

Mutex m1 = Mutex.OpenExisting("mymutex");

m1.WaitOne();

// Use R1 and R2.

m1.ReleaseMutex();


--
William Stacey [MVP]

| Hi William,
| thanks for replying, the requirement is:
| 1. there's 2 resources (R1 & R2), which need to be accessed in a serial
| mode
| 2. access to this resources is coming from 3 different multithreaded
| process (P1, P2, P3).
| I try to solve this requirement by using mutexes,
| P1 using the R1, it locked this resources, so R1 can't be used until P1
| is releasing R1.
| The question is: by using Mutex.WaitAny for queueing P1, P2, P3
| accessing R1 & R2, I can't find out whether R1 or R2 is used, so I
| don't know which Mutex to be released. If this kind of requirement
| can't be solved by using mutexes, any other brief solution is highly
| appreciated.
|
| Best Regards,
|
 
Actually P1, P2, P3 is 3 different application.
Yes, that's what I need, using OpenExisting method to syncrhonize mutex
between different process / application / instance.
Regarding R1 & R2, the problem is each process using whichever
resources available
(R1 / R2), not both. That's why I think it's appropriate to use
WaitAny method for synchronizing resources. The code somehow like
this:

Mutex [] m = new Mutex[2];
m1 = Mutex.OpenExisting("mymutex");
m2 = Mutex.OpenExisting("mymutex2");
m[0] = m1;
m[1] = m2;
// Do error handling

// P1,2,3 will get existing mutex and wait on it (timeout recommended),
do
work, and release it.
//Mutex m1 = Mutex.OpenExisting("mymutex");
//if i changed this code : m1.WaitOne(); to:
Mutex.WaitAny(m);

// Use R1 or R2, whichever available
// here is the part where i confused, if using WaitAny like above,
// how could i know that the one that I used is m1 / m2?
// if using m1 then:
m1.ReleaseMutex();
// else if using m2 then:
m2.ReleaseMutex();
 
WaitAny returns the index of the mutex that signaled - so that is how you
know which one to release later.

--
William Stacey [MVP]

| // Use R1 or R2, whichever available
| // here is the part where i confused, if using WaitAny like above,
| // how could i know that the one that I used is m1 / m2?
| // if using m1 then:
| m1.ReleaseMutex();
| // else if using m2 then:
| m2.ReleaseMutex();
|
 

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

Back
Top