Thread safe list?

Can it be considered thread safe due to the appearance of an atomic code operation. My main problem is that the lists have to be redistributed and when recalibrated it becomes non-protein.

List<int> list = new List<int>(10);

public List<int> GetList()
{
  var temp = list;
  list = new List<int>(10);
  return temp;
}

TimerElapsed(int number)
{
  list.Add(number);
}
+3
source share
6 answers

Neither your code nor List<T>thread safe.

The list is not thread safe according to its documentation . Your code is not thread safe because it lacks synchronization.

, GetList . , temp. temp, list GetList. , list, .

! TimerElapsed , , , . , , .

+3

. List<T> , :

, , . , . , . . System.Collections.Concurrent. , , . ImmutableList.

+4

. ThreadSafe.

System.Collections.Concurrent

+1
+1

, GetList , TimerElapsed. . , List , System.Collections.Concurrent.

0

, .

-1

All Articles