Warning: This API is deprecated and will be removed in a future
version of TensorFlow after
the replacement is stable.
MutexLock
Stay organized with collections
Save and categorize content based on your preferences.
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.
Public Methods
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.
|
Inherited Methods
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()
|
Public Methods
public
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.
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<?>
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.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2022-02-12 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2022-02-12 UTC."],[],[],null,["# MutexLock\n\npublic final class **MutexLock** \nLocks a mutex resource. The output is the lock. So long as the lock tensor\n\n\nis alive, any other request to use \\`MutexLock\\` with this mutex will wait.\n\n\nThis is particularly useful for creating a critical section when used in\nconjunction with \\`MutexLockIdentity\\`: \n\n mutex = mutex_v2(\n shared_name=handle_name, container=container, name=name)\n \n def execute_in_critical_section(fn, *args, **kwargs):\n lock = gen_resource_variable_ops.mutex_lock(mutex)\n \n with ops.control_dependencies([lock]):\n r = fn(*args, **kwargs)\n \n with ops.control_dependencies(nest.flatten(r)):\n with ops.colocate_with(mutex):\n ensure_lock_exists = mutex_lock_identity(lock)\n \n # Make sure that if any element of r is accessed, all of\n # them are executed together.\n r = nest.map_structure(tf.identity, r)\n \n with ops.control_dependencies([ensure_lock_exists]):\n return nest.map_structure(tf.identity, r)\n \nWhile \\`fn\\` is running in the critical section, no other functions which wish to use this critical section may run.\n\n\nOften the use case is that two executions of the same graph, in parallel,\nwish to run \\`fn\\`; and we wish to ensure that only one of them executes\nat a time. This is especially important if \\`fn\\` modifies one or more\nvariables at a time.\n\n\nIt is also useful if two separate functions must share a resource, but we\nwish to ensure the usage is exclusive.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n### Public Methods\n\n|---------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [Output](/api_docs/java/org/tensorflow/Output)\\\u003cObject\\\u003e | [asOutput](/api_docs/java/org/tensorflow/op/core/MutexLock#asOutput())() Returns the symbolic handle of a tensor. |\n| static [MutexLock](/api_docs/java/org/tensorflow/op/core/MutexLock) | [create](/api_docs/java/org/tensorflow/op/core/MutexLock#create(org.tensorflow.op.Scope,%20org.tensorflow.Operand\u003c?\u003e))([Scope](/api_docs/java/org/tensorflow/op/Scope) scope, [Operand](/api_docs/java/org/tensorflow/Operand)\\\u003c?\\\u003e mutex) Factory method to create a class wrapping a new MutexLock operation. |\n| [Output](/api_docs/java/org/tensorflow/Output)\\\u003c?\\\u003e | [mutexLock](/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. |\n\n### Inherited Methods\n\nFrom class [org.tensorflow.op.PrimitiveOp](/api_docs/java/org/tensorflow/op/PrimitiveOp) \n\n|------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------|\n| final boolean | [equals](/api_docs/java/org/tensorflow/op/PrimitiveOp#equals(java.lang.Object))(Object obj) |\n| final int | [hashCode](/api_docs/java/org/tensorflow/op/PrimitiveOp#hashCode())() |\n| [Operation](/api_docs/java/org/tensorflow/Operation) | [op](/api_docs/java/org/tensorflow/op/PrimitiveOp#op())() Returns the underlying [Operation](/api_docs/java/org/tensorflow/Operation) |\n| final String | [toString](/api_docs/java/org/tensorflow/op/PrimitiveOp#toString())() |\n\nFrom class java.lang.Object \n\n|------------------|---------------------------|\n| boolean | equals(Object arg0) |\n| final Class\\\u003c?\\\u003e | getClass() |\n| int | hashCode() |\n| final void | notify() |\n| final void | notifyAll() |\n| String | toString() |\n| final void | wait(long arg0, int arg1) |\n| final void | wait(long arg0) |\n| final void | wait() |\n\nFrom interface [org.tensorflow.Operand](/api_docs/java/org/tensorflow/Operand) \n\n|-------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------|\n| abstract [Output](/api_docs/java/org/tensorflow/Output)\\\u003cObject\\\u003e | [asOutput](/api_docs/java/org/tensorflow/Operand#asOutput())() Returns the symbolic handle of a tensor. |\n\nPublic Methods\n--------------\n\n#### public [Output](/api_docs/java/org/tensorflow/Output)\\\u003cObject\\\u003e\n**asOutput**\n()\n\nReturns the symbolic handle of a tensor.\n\nInputs to TensorFlow operations are outputs of another TensorFlow operation. This method is\nused to obtain a symbolic handle that represents the computation of the input.\n\n\u003cbr /\u003e\n\n#### public static [MutexLock](/api_docs/java/org/tensorflow/op/core/MutexLock)\n**create**\n([Scope](/api_docs/java/org/tensorflow/op/Scope) scope, [Operand](/api_docs/java/org/tensorflow/Operand)\\\u003c?\\\u003e mutex)\n\nFactory method to create a class wrapping a new MutexLock operation. \n\n##### Parameters\n\n| scope | current scope |\n| mutex | The mutex resource to lock. |\n|-------|-----------------------------|\n\n##### Returns\n\n- a new instance of MutexLock \n\n#### public [Output](/api_docs/java/org/tensorflow/Output)\\\u003c?\\\u003e\n**mutexLock**\n()\n\nA tensor that keeps a shared pointer to a lock on the mutex;\nwhen the Tensor is destroyed, the use count on the shared pointer is decreased\nby 1. When it reaches 0, the lock is released."]]