Share via

OwinHttpListener threading issue

satheesh 1 Reputation point
2022-10-12T19:46:38.147+00:00

I am looking at source code of OwinHttpListener. I particularly want to understand two things

Why updating of CanAcceptMoreRequests variable is using Interlocked but not the read in both ProcessRequestsAsync as well as in OffloadStartNextRequest methods.

In the OffloadStartNextRequest method is spinning a new thread when the condition _listener.IsListening && CanAcceptMoreRequests is successful and calling ProcessRequestsAsync which has a while loop on the same condition. In ProcessRequestsAsync method, it waits for HTTP request and before processing request it is again calling OffloadStartNextRequest which in turn calls ProcessRequestsAsync method again in a new thread and potentially same chain happens as more requests comes in quickly. Does it not flood with threads executing same code with while loop

What am I missing?

Developer technologies | ASP.NET Core | Other
0 comments No comments

3 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 84,086 Reputation points
    2022-10-16T20:15:22.94+00:00

    interlocked.read is for reading a 64 bit integer with a 32 bit app.

    Was this answer helpful?

    0 comments No comments

  2. satheesh 1 Reputation point
    2022-10-15T08:09:12.39+00:00

    Thanks. I am aware of increment/decrement.. Question was mostly for read.. So you say that interlocked.read is only for long and not required for int
    In regards to second question, i found the answer myself with a test application and that part is clear to me

    Was this answer helpful?

    0 comments No comments

  3. Bruce (SqlWork.com) 84,086 Reputation points
    2022-10-13T20:13:55.547+00:00

    if using integers, you do not need to lock the read, only the write. the interlocked method calls a low level hardware command that implements a safe increment and decrement.

    the interlocked counters are used to limit the thread creation.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.