JustAnotherCoder
JustAnotherCoder

Reputation: 153

Calls between static methods - will that foil thread safety?

In a public static class I have the following local method:

private static int GetMaxFromList(IEnumerable<int> list)
    {
        var result = 0;

        foreach (var i in list)
        {
            if (i > result) result = i;
        }

        return result;
    }

This method is called by some of the public methods within the class (example):

public static bool IsIntegrityOfDataSetGood(DataSet dataSet, KeyValuePair<string, List<int>> tableAndColumnIndexes)
    {
        return IsIntegrityOfDataSetGood(dataSet, tableAndColumnIndexes.Key) &&
               dataSet.Tables[tableAndColumnIndexes.Key].Columns.Count > GetMaxFromList(tableAndColumnIndexes.Value);
    }

Note that this public method also calls another public method within the class.

The class methods only work with data sent to them through parameters, but I am worried that the calls between the static methods will mess up thread safety. Am I right to worry?

Forgot to mention that this code resides in a .NET 2.0 project.

Upvotes: 2

Views: 222

Answers (2)

Jon Skeet
Jon Skeet

Reputation: 1503749

So long as no other threads are changing the list sequence, it should be absolutely fine. It doesn't access any shared state - it just uses its parameter. If any other thread is modifying the sequence, you'll get an InvalidOperationException.

It sounds like you may have some misconceptions about thread safety and static methods, but it's hard to tell exactly what they are... you may find Eric Lippert's blog post "What is this thing you call thread safe?" useful.

Upvotes: 3

Henk Holterman
Henk Holterman

Reputation: 273824

No, this will not foil your thread-safety.

But whether it really is thread-safe depends on where tableAndColumnIndexes comes from and what other Threads have access to it. In other words, it depends on the calling code.

Upvotes: 4

Related Questions