I achknowledge the fact I have to lock a hashset to make it thread safe when working inside of multiple threads, what I don't really understand is what if you have multiple hashsets? Can you use the same lock object? Or would you need a seperate object?
For example... I have these 2 methods, that use the same object.
private void RemoveSheduledAttack(Attack attack)
{
lock (_syncRoot)
{
_sheduledAttacks.Remove(attack);
}
}
private void AddActiveAttack(Attack attack)
{
lock (_syncRoot)
{
ActiveAttacks.Add(attack);
}
}
Declaring:
private readonly HashSet<Attack> _sheduledAttacks;
private static object _syncRoot = new object();
public HashSet<Attack> ActiveAttacks;