Recently I had an interview question in a test that was similar to the one below, I don't have much experience with thread development, can someone please advise me on how to approach this question ?:
public class StringQueue { private object _lockObject = new object(); private List<string> _items = new List<string>(); public bool IsEmpty() { lock (_lockObject) return _items.Count == 0; } public void Enqueue(string item) { lock (_lockObject) _items.Add(item); } public string Dequeue() { lock (_lockObject) { string result = _items[0]; _items.RemoveAt(0); return result; } } }
Is the following method a thread safe with the above implementation and why?
public string DequeueOrNull() { if (IsEmpty()) return null; return Dequeue(); }
multithreading c #
user834442
source share