This function creates a thread-safe version of f. In particular, it takes a mutex and returns a new function that locks it when the function is called and unlocks it when complete, even if there is an error.
If mutex is omitted, then a mutex is created behind the scenes. Therefore, it's only necessary to define mutex before calling lock if you plan on sharing it between multiple functions.
Consider the following example.
|
|
|
|
You likely see that x ended up less than the expected value of 1000. This is an example of a race condition, as multiple threads were trying to modify the global variable x at the same time.
Let's try again using lock.
|
|
|
|
The source of this document is in /build/reproducible-path/macaulay2-1.26.06+ds/M2/Macaulay2/packages/Macaulay2Doc/doc_mutex.m2:138:0.