@@ -29,10 +29,11 @@ def __getitem__(self, key: K) -> V:
2929 def __setitem__ (self , key : K , value : V ) -> None :
3030 warnings .warn (
3131 f"Direct assignment (D[key] = value) is discouraged. "
32- f"Use update_atomic for thread-safe compound updates." ,
32+ f"Use assign_atomic() for assigning a value to a new key safely, "
33+ f"or update_atomic() for thread-safe update of an existing dictionary key." ,
3334 stacklevel = 2
3435 )
35- self .update_atomic (key , lambda _ : value )
36+ self .assign_atomic (key , value )
3637
3738
3839 def __delitem__ (self , key : K ) -> None :
@@ -50,17 +51,27 @@ def setdefault(self, key: K, default: V) -> V:
5051 return self ._dict .setdefault (key , default )
5152
5253
54+ def assign_atomic (self , key : K , value : V ) -> None :
55+ """
56+ Atomically assign a value to a key.
57+
58+ This method ensures that the assignment is performed atomically,
59+ preventing
60+ """
61+ self .update_atomic (key , lambda _ : value )
62+
63+
5364 def update_atomic (self , key : K , func : Callable [[V ], V ]) -> None :
5465 """
55- Atomically update the value for a key using func(old_value) -> new_value.
66+ Atomically modify the value for a key using func(old_value) -> new_value.
5667
5768 This method ensures that the read-modify-write sequence is performed atomically,
5869 preventing race conditions in concurrent environments.
5970
6071 Example:
6172 d = ConcurrentDictionary({'x': 0})
6273 # Atomically increment the value for 'x'
63- d.update_atomic ('x', lambda v: v + 1)
74+ d.modify_atomic ('x', lambda v: v + 1)
6475 """
6576 with self ._lock :
6677 if key in self ._dict :
0 commit comments