@Operator public final class MutexLock extends PrimitiveOp implements Operand<Object>
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.
operation
Modifier and Type | Method and Description |
---|---|
Output<Object> |
asOutput()
Returns the symbolic handle of a tensor.
|
static MutexLock |
create(Scope scope,
Operand<?> mutex)
Factory method to create a class wrapping a new MutexLock operation.
|
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.
|
equals, hashCode, op, toString
public static MutexLock create(Scope scope, Operand<?> mutex)
scope
- current scopemutex
- The mutex resource to lock.public Output<?> mutexLock()
public Output<Object> asOutput()
Operand
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.
asOutput
in interface Operand<Object>
OperationBuilder.addInput(Output)
Copyright © 2022. All rights reserved.