none
Thread Safety

    Question

  • Hi, I am using the following Configuration(ServiceBehaviour) for my Restful WCF service:

    InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple

    Now I have the following method in my service implementation class:

    public object MyMethod(MutableObject mutableObject)
            {
                RequestHandler handler = new MyMethodHandler(mutableObject);
                return handler.ProcessRequest();
            }

    Can anyone tell me if the above method calls will be thread safe, as many threads can call the same many method at the same time.

    Thanks

    Thursday, June 28, 2012 7:51 AM

Answers

  • Thank you for the briefing. Now I have bit more detail about your scenario in order to help you further.Yes calling method in this case is thread safe.

    Extra note for you: The possibility of data race which I talked in my last message was to do with sharing any local variables (with in service implementation) among thread/service call. I have also included a link on throttling and instancing which act as catalyst of WCF multi threading. 



    Please click "Mark As Answer" if it serve the purpose. Regards, Nairs

    • Marked as answer by Yi-Lun Luo Monday, July 09, 2012 2:35 AM
    Friday, June 29, 2012 8:59 AM

All replies

  • In this service combination, one WCF service instance is created for serving all WCF clients. All requests are served using multiple / different threads. I.e. your service processes many requests but you are responsible for ensuring your code is thread safe.

    From your code it seems you are not using any shared resources so you do not need any synchronization context or lock. If you have or planning to use shared resources- you must synchronize access to the service instance or risk state corruption.


    Lingaraj Mishra


    Thursday, June 28, 2012 1:31 PM
  • Thanks for reply. So you are saying that even though the method parameter passed is "mutable reference type" still the method is thread safe. Also if in the handler.ProcessRequest method if I am creating another object which has instance variables and method, still the call to MyMethod is thread safe?

    Thanks

    Thursday, June 28, 2012 6:28 PM
  • Your code 

    InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple

    ask WCF runtime to dispatch one thread each for each service call which eventually access a single instance of your service implementation. Here if you forgive the input parameter for the time being then the MyMethod is thread safe. Because it doesn't access any data that could be accessed by another thread. This is due to the fact that every thread has it's on thread stack, and local variables are not shared among stacks. Therefore your method is thread safe at this stage.

    Now lets consider input parameter. What happens if the supplied parameter reference type ? Here comes the trouble of data race. Because it is very like that you end up in data race if the supplied parameter is of reference type. I have no idea whether the supplied MutableObject is a Value or reference type. But I do have confident that you can decide the outcome from my earlier briefing.

    Regards

    Nairs


    • Edited by Nair S Thursday, June 28, 2012 10:22 PM Sign
    Thursday, June 28, 2012 10:16 PM
  • Thanks Nairs for reply. Actually method parameter MutableObject is a reference type. Just to give you an idea how it being called in my test application: As for each client request, I am getting this MutableObject(deserilizing XML request to MutableObject), so a new thread will call MyMethod which will have this this MutableObject as a parameter. Can you please explain little bit more how it can have data race trouble in this scenario.

    Thanks

    Thursday, June 28, 2012 11:44 PM
  • Thank you for the briefing. Now I have bit more detail about your scenario in order to help you further.Yes calling method in this case is thread safe.

    Extra note for you: The possibility of data race which I talked in my last message was to do with sharing any local variables (with in service implementation) among thread/service call. I have also included a link on throttling and instancing which act as catalyst of WCF multi threading. 



    Please click "Mark As Answer" if it serve the purpose. Regards, Nairs

    • Marked as answer by Yi-Lun Luo Monday, July 09, 2012 2:35 AM
    Friday, June 29, 2012 8:59 AM