一款简单易用的基础网络通讯组件库。

SystemThreadingExtensionApplyChangeOptimisticallyT, TArg(T, TArg, FuncT, TArg, T) 方法

Optimistically performs some value transformation based on some field and tries to apply it back to the field, retrying as many times as necessary until no other thread is manipulating the same field.

Definition

命名空间: TouchSocket.Core
程序集: TouchSocket.Core (在 TouchSocket.Core.dll 中) 版本:4.0.0-beta.143+5fcca7a4e70968794985dc3793adc50531308be6
C#
public static bool ApplyChangeOptimistically<T, TArg>(
	ref T hotLocation,
	TArg applyChangeArgument,
	Func<T, TArg, T> applyChange
)
where T : class

参数

hotLocation  T
The field that may be manipulated by multiple threads.
applyChangeArgument  TArg
An argument to pass to applyChange.
applyChange  FuncT, TArg, T
A function that receives both the unchanged value and applyChangeArgument, then returns the changed value.

类型参数

T
The type of data to apply the change to.
TArg
The type of argument passed to the applyChange.

返回值

Boolean
if the location's value is changed by applying the result of the applyChange function; if the location's value remained the same because the last invocation of applyChange returned the existing value.

备注

Use this overload when applyChange requires a single item, as is common when updating immutable collection types. By passing the item as a method operand, the caller may be able to avoid allocating a closure object for every call.

参见