SocketMutex: The SocketMutex class

Description Usage Arguments Fields and Methods Why a socket connection to a server? Author(s) References Examples

Description

Package: R.synchronize
Class SocketMutex

Object
~~|
~~+--AbstractMutex
~~~~~~~|
~~~~~~~+--SocketMutex

Directly known subclasses:

public static class SocketMutex
extends AbstractMutex

A SocketMutex is a class for acquiring a mutex from a server.

Usage

1
SocketMutex(host="127.0.0.1", port=7777, ...)

Arguments

host, port

The hostname and port number of the server.

...

Not used.

Fields and Methods

Methods:

finalize -
getHost -
getPort -
isAcquired -
release -
tryAcquire -

Methods inherited from AbstractMutex:
acquire, finalize, isAcquired, release, tryAcquire

Methods inherited from Object:
$, $<-, [[, [[<-, as.character, attach, attachLocally, clearCache, clone, detach, equals, extend, finalize, gc, getEnvironment, getFields, getInstantiationTime, getStaticInstance, hasField, hashCode, ll, load, objectSize, print, save

Why a socket connection to a server?

A TCP socket connection guarantees that only one client is connected to the server at any time [1]. Thus, with multiple clients sequentially connecting and disconnecting to the server, we have the basic functionality of a queue that is guaranteed to only serve one client at any time. In other words, this will allow us to setup a server that is guaranteed to hand out the mutex to at most one client at any time. Regardless how hard the clients tries to request the mutex, race conditions will never occur.

Author(s)

Henrik Bengtsson (http://www.braju.com/R/)

References

[1] John Nielsen, Recipe 408997: When to not just use socket.close(), ActiveState Code, April 2005. http://code.activestate.com/recipes/408997/

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
## Not run: 
  
# Run a seperate R process providing a mutex server
srv <- SocketMutexServer(port=7777);
run(srv, verbose=-10);


  
# Run an R process to wait for a mutex, do some
# job that requires atomic transactions, release the
# mutex, and so on.

# Server that controls the mutex
host <- "127.0.0.1"

mutex <- SocketSemaphore(host, port=7777)
for (kk in 1:50) {
  if (acquire(mutex)) {
    cat("Client acquired mutex.\n")
    Sys.sleep(runif(n=1, max=0.01))
    release(mutex)
    cat("Client released mutex.\n")
    Sys.sleep(runif(n=1, max=0.2))
  }
}

 
## End(Not run)

R.synchronize documentation built on May 2, 2019, 5:46 p.m.