Skip to content

sebnilsson/KeyLocks

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 

KeyLocks

Run code with locks based on different object-types, like strings, numbers & dates.

Selectively lock code on specific values, not globally for all executing code.

private static KeyLock<string> _keyLock = new KeyLock<string>();

public void Main()
{
    Parallel.Invoke(
        () => { UpdateData("entity-123", "First new value"); },
        () => { UpdateData("entity-123", "Second new value"); }, // This will await line above
        () => { UpdateData("another-entity-456", "Another new value"); },        
        () => { UpdateData("yet-another-entity-789", "Yet another new value"); });
}

private void UpdateData(string id, string value)
{
    _keyLock.RunWithLock(id, () =>
    {
        // Execute locked code
    });
}

Method 1 and 2 executed inside the Parallel.Invoke, which are executing the UpdateData-method, will await eachother and not run simultainously, while all other will run in parallel.

NameLock

The type NameLock is a short-hand term for KeyLock<string>.

It defaults to being case-sensitive, but that can be changed by passing StringComparer.InvariantCultureIgnoreCase as a constructor-argument.

About

Run code with locks based on different object-types, like strings, numbers & dates

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages