简体   繁体   中英

All Callbacks on GUI Thread - Multithreading issues possible?

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?

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:

  • Must work only on the data provided to it by the caller.

Therefore it is not reentrant. The accepted answer points out why it's not thread safe either.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM