Skip to content

Conversation

@bgrainger
Copy link
Contributor

There was a rogue U+200B character that was rendering as mojibake:

image

Also update the MSDN link in this documentation to avoid an unnecessary redirection and use the correct anchor.

Update the MSDN link to avoid an unnecessary redirection and use the correct anchor.
@bgrainger bgrainger requested a review from a team as a code owner August 28, 2023 18:04
Copy link
Contributor

@michaelnebel michaelnebel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bgrainger : Thank you for doing this!
LGTM

@github-actions
Copy link
Contributor

QHelp previews:

csharp/ql/src/Concurrency/UnsynchronizedStaticAccess.qhelp

Unsynchronized access to static collection member in non-static context

For performance reasons, most collection classes in the standard library are not thread-safe, instead requiring the user to guarantee they are used from at most one thread at a time by external locking or data structure invariants.

For example, the behavior of Dictionary when a write happens concurrently with another write or a read is undefined, and frequently leads to data corruption and can lead to issues as serious as livelock.

Recommendation

If a static data member such as a Dictionary is likely to be accessed from multiple threads, ensure that either it is of a concurrency-safe collection type, or that all reads and writes are guarded by a suitable lock or monitor.

Example

The following code uses a static dictionary to store properties, but provides unsynchronized access to that dictionary. This means that multiple threads can access the dictionary, potentially leading to a race condition.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading;

public class Configuration
{
    public static Dictionary<string, string> properties = new Dictionary<string, string>();

    // called concurrently elsewhere
    public string getProperty(string key)
    {
        // BAD: unsynchronized access to static collection
        return dict["foo"];
    }
}

References

@michaelnebel michaelnebel merged commit dd27442 into github:main Aug 29, 2023
@bgrainger bgrainger deleted the fix-unsynchronized-static-access-docs branch August 29, 2023 11:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants