Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multi-threading with abstract protected member?

I'm considering an existing multi-threading C# application and noticed some code like

public abstract class Task
{
    protected IList<string> action;

...

    public void Initialize (params)
    ...

Several special "Tasks" derive from Task and some Task methods modify "action". So in my idea, this is not thread safe. Am I right ?

Thanks for any help,

Olivier

like image 549
user1875921 Avatar asked Apr 23 '26 14:04

user1875921


1 Answers

No, you are not right. action is an instance field, so each instance of the Task-derived class will have its own copy of action. It would potentially be a problem only if action were static.

To clarify, you can have many Task-derived objects. Each one of those will work just fine for a single thread. However, you do not want to have multiple threads accessing the same Task instance, unless the code that modifies action is protected with some kind of synchronization.

like image 190
Jim Mischel Avatar answered Apr 26 '26 05:04

Jim Mischel