Ok, newbie multithreaded question:
I have a Singleton class. The class has a static list and essentially works as follows:
class MyClass {
private static MyClass _instance;
private static List<string> _list;
private static bool IsRecording;
public static void StartRecording() {
_list = new List<string>();
IsRecording = true;
}
public static IEnumerable<string> StopRecording() {
IsRecording = false;
return new List<string>(_list).AsReadOnly();
}
public MyClass GetInstance(){
}
public void DoSomething(){
if(IsRecording) _list.Add("Something");
}
}
In principle, the user can call StartRecording () to initialize the list, and then all calls to the instance method can add material to the list. However, multiple threads may contain an instance for MyClass, so multiple threads may add entries to the list.
However, creating and reading a list is a single operation, so the usual read-write problem in multi-threaded situations does not apply. The only problem I saw was the insertion order, which is strange, but it is not a problem.
, - ? , , ( _list.Add( (somedata))), , , to DateTime.Now.
: , : DoSomething ( , , -, ).
lock(_list){
_list.Add(something);
}
and
lock(_list){
return new List<string>(_list).AsReadOnly();
}
?