Reputation: 3009
We have an external data provider which, in its construtor, takes a callback thread for returning data upon.
There are some issues in the system which I am suspicious are related to threading, however, in theory they cannot be, due to the fact that the callbacks should all be returned on the same thread.
My question is, does code like this require thread synchronisation?
class Foo
{
ExternalDataProvider _provider;
public Foo()
{
// This is the c'tor for the xternal data provider, taking a callback loop as param
_provider = new ExternalDataProvider(UILoop);
_provider.DataArrived += ExternalProviderCallbackMethod;
}
public ExternalProviderCallbackMethod(...)
{
//...(code omitted)
var itemArray[] = new String[4] { "item1", "item2", "item3", "item4" };
for (int i = 0; i < itemArray.Length; i++)
{
string s = itemArray[i];
switch(s)
{
case "item1":
DoItem1Action();
break;
case "item2":
DoItem2Action();
break;
default:
DoDefaultAction();
break;
}
//...(code omitted)
}
}
}
The issue is that, very infrequently, DoItem2Action is executingwhen DoItem1Action should be exectuing.
Is it at all possible threading is at fault here? In theory, as all callbacks are arriving on the same thread, they should be serialized, right? So there should be no need for thread sync here?
Upvotes: 0
Views: 374
Reputation: 48486
Even if these callbacks weren't on the same thread, they couldn't be the cause of your problem, as your callback method is fully reentrant. There is nothing for you to synchronize.
The problem must be somewhere else, or I'm not understanding your problem correctly :)
PS: in relation to the other threading question you posted, it violates this rule:
Therefore it is not reentrant. The accepted answer points out why it's not thread safe either.
Upvotes: 1