Qaurk Noble said:
Hello all,
Does anyone know how I can create a mutex to control access to a shared
resource on one computer?
Regards,
Q. Noble
It depends on where you want to control access from:
1. All clients in a single JVM -- that is all clients of that shared
resource have their lifecycles within a single JVM
or
2. The clients are in different JVMs.
(or)
2b. The clients are different entities, at least one of which is not even a
JVM.
1. is the easiest because any object can be used as a mutex. If the shared
resource is represented by an Object, simply synchronizing around it will do
the trick.
2 and 2b. require that you use some OS specific synchronization resource.
For example, you can use a UDP or a TCP socket which is bound to a
particular port as a mutex. To gain control of the shared resource, a
client must gain control of the mutex first. In this case, this can be done
by opening a socket at a chosen port number. If the client suceeds in doing
so, it can assume access to the shared resource. When done, it closes the
socket. Other clients can then make use of the shared resource.
Of course, the above are simplistically stated, but I hope they give you
some ideas.
HTH,