# MutexLock

public final class **MutexLock** Locks a mutex resource. The output is the lock. So long as the lock tensor


is alive, any other request to use \`MutexLock\` with this mutex will wait.


This is particularly useful for creating a critical section when used in
conjunction with \`MutexLockIdentity\`:

    mutex = mutex_v2(
       shared_name=handle_name, container=container, name=name)
     
     def execute_in_critical_section(fn, *args, **kwargs):
       lock = gen_resource_variable_ops.mutex_lock(mutex)
     
       with ops.control_dependencies([lock]):
         r = fn(*args, **kwargs)
     
       with ops.control_dependencies(nest.flatten(r)):
         with ops.colocate_with(mutex):
           ensure_lock_exists = mutex_lock_identity(lock)
     
         # Make sure that if any element of r is accessed, all of
         # them are executed together.
         r = nest.map_structure(tf.identity, r)
     
       with ops.control_dependencies([ensure_lock_exists]):
         return nest.map_structure(tf.identity, r)
     
While \`fn\` is running in the critical section, no other functions which wish to use this critical section may run.


Often the use case is that two executions of the same graph, in parallel,
wish to run \`fn\`; and we wish to ensure that only one of them executes
at a time. This is especially important if \`fn\` modifies one or more
variables at a time.


It is also useful if two separate functions must share a resource, but we
wish to ensure the usage is exclusive.

<br />

<br />

<br />

<br />

### Public Methods

|---|---|
| [Output](https://www.tensorflow.org/api_docs/java/org/tensorflow/Output)\<Object\> | [asOutput](https://www.tensorflow.org/api_docs/java/org/tensorflow/op/core/MutexLock#asOutput())() Returns the symbolic handle of a tensor. |
| static [MutexLock](https://www.tensorflow.org/api_docs/java/org/tensorflow/op/core/MutexLock) | [create](https://www.tensorflow.org/api_docs/java/org/tensorflow/op/core/MutexLock#create(org.tensorflow.op.Scope,%20org.tensorflow.Operand<?>))([Scope](https://www.tensorflow.org/api_docs/java/org/tensorflow/op/Scope) scope, [Operand](https://www.tensorflow.org/api_docs/java/org/tensorflow/Operand)\<?\> mutex) Factory method to create a class wrapping a new MutexLock operation. |
| [Output](https://www.tensorflow.org/api_docs/java/org/tensorflow/Output)\<?\> | [mutexLock](https://www.tensorflow.org/api_docs/java/org/tensorflow/op/core/MutexLock#mutexLock())() A tensor that keeps a shared pointer to a lock on the mutex; when the Tensor is destroyed, the use count on the shared pointer is decreased by 1. |

### Inherited Methods

From class [org.tensorflow.op.PrimitiveOp](https://www.tensorflow.org/api_docs/java/org/tensorflow/op/PrimitiveOp)

|---|---|
| final boolean | [equals](https://www.tensorflow.org/api_docs/java/org/tensorflow/op/PrimitiveOp#equals(java.lang.Object))(Object obj) |
| final int | [hashCode](https://www.tensorflow.org/api_docs/java/org/tensorflow/op/PrimitiveOp#hashCode())() |
| [Operation](https://www.tensorflow.org/api_docs/java/org/tensorflow/Operation) | [op](https://www.tensorflow.org/api_docs/java/org/tensorflow/op/PrimitiveOp#op())() Returns the underlying `https://www.tensorflow.org/api_docs/java/org/tensorflow/Operation` |
| final String | [toString](https://www.tensorflow.org/api_docs/java/org/tensorflow/op/PrimitiveOp#toString())() |

From class java.lang.Object

|---|---|
| boolean | equals(Object arg0) |
| final Class\<?\> | getClass() |
| int | hashCode() |
| final void | notify() |
| final void | notifyAll() |
| String | toString() |
| final void | wait(long arg0, int arg1) |
| final void | wait(long arg0) |
| final void | wait() |

From interface [org.tensorflow.Operand](https://www.tensorflow.org/api_docs/java/org/tensorflow/Operand)

|---|---|
| abstract [Output](https://www.tensorflow.org/api_docs/java/org/tensorflow/Output)\<Object\> | [asOutput](https://www.tensorflow.org/api_docs/java/org/tensorflow/Operand#asOutput())() Returns the symbolic handle of a tensor. |

## Public Methods

#### public [Output](https://www.tensorflow.org/api_docs/java/org/tensorflow/Output)\<Object\>
**asOutput**
()

Returns the symbolic handle of a tensor.

Inputs to TensorFlow operations are outputs of another TensorFlow operation. This method is
used to obtain a symbolic handle that represents the computation of the input.

<br />

#### public static [MutexLock](https://www.tensorflow.org/api_docs/java/org/tensorflow/op/core/MutexLock)
**create**
([Scope](https://www.tensorflow.org/api_docs/java/org/tensorflow/op/Scope) scope, [Operand](https://www.tensorflow.org/api_docs/java/org/tensorflow/Operand)\<?\> mutex)

Factory method to create a class wrapping a new MutexLock operation.

##### Parameters

| scope | current scope |
| mutex | The mutex resource to lock. |
|---|---|

##### Returns

- a new instance of MutexLock

#### public [Output](https://www.tensorflow.org/api_docs/java/org/tensorflow/Output)\<?\>
**mutexLock**
()

A tensor that keeps a shared pointer to a lock on the mutex;
when the Tensor is destroyed, the use count on the shared pointer is decreased
by 1. When it reaches 0, the lock is released.