I have a function that, for argument sake, has two lines of code.
Line A
Line B
Both lines are calls to third party web service that does some work. It appears as though the service call on Line B is contingent on being called immediately (time-wise) after the call to Line A. This all works fine in a non-threaded environment but my application is threading lots (potentially 100) of these calls.
The problem with this threading, I believe, is that the context switching between the threads is causing enough time (a very small amount of time) to elapse between the call on Line A and the call on Line B that it’s causing the call on Line B to throw a custom soap exception.
My knowledge of threading doesn’t really extend to a situation like this. Is there anyway to make sure the call on Line B happens immediately after the call on Line A without thread context switching occurring in between?
If you need these lines to be called together then you can lock them, meaning that nothing else would enter this section until it is complete:
However, this may cause you problems if this is all the 100 threads are doing – you lose your parallelism. In fact, whatever you do, if this is a general requirement, you will lose a degree of parallelism.