I have an IronPython script that uses TPL and Parallel.ForEach to process files using multiple threads. In C #, I can use Interlocked.Add and Interlocked.Increment to change global variables in an atomic thread-safe operation, but this does not work in IronPython because integers are immutable . I currently have a simple Results class that stores several variables as static members, which are used to track the results of a multithreaded operation. When changing multiple values, I can lock the class using the .NET Monitor class to ensure that the update is thread safe, but this seems like a lot of overhead if I only want to update one variable (for example, just increment the results. Files).
My question is, is there a better way to increment a single static member variable, like Results.Files in IronPython, in thread safe or atomic form, similar to how Interlocked.Increment works? Also, are there thread-safe counters built into the python or .NET framework that can be used in place of the base integer?
class Results: Files = 0 Lines = 0 Tolkens = 0 @staticmethod def Add(intFiles, intLines, intTolkens):
Greg Bray Feb 12 '10 at 22:11 2010-02-12 22:11
source share