in a project im playing around with threads. Im trying to make a safe thread that dosn't 'corrupt' the data. My thread runs in the background and call functions on an other class, I can call Go and Go2, one function adds and one deletes from a list. I dont want them to run at the same time, what is the difference between the following situation:
static readonly object _locker1 = new object();
static readonly object _locker2 = new object();
public void Go(Object something)
{
lock (_locker1)
{
myList.add(something);
}
}
public void Go2(Object something)
{
lock (_locker2)
{
myList.Remove(something);
}
}
And if i would replace Go2 with:
public void Go2(Object something)
{
lock (_locker1)
{
myList.Remove(something);
}
}
Note the lock parameter.
A third situation that would help me understand, lets say i call Go from a different thread (thread2), can it run because _locker1 is locked by thread2 and Go2 (which has _locker 1 that is locked by thread2) is called from thread1?
static readonly object _locker1 = new object();
static readonly object _locker2 = new object();
public void Go(Object something)
{
lock (_locker1)
{
//Can I call Go2 which is locked by the same object?
Go2(something);
}
}
public void Go2(Object something)
{
lock (_locker1)
{
myList.Remove(something);
}
}
Could someone explain what the value passed to lock does?