Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7446507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:20:01+00:00 2026-05-29T12:20:01+00:00

Hoping someone can help me design this correctly. In my TCP code, I have

  • 0

Hoping someone can help me design this correctly.

In my TCP code, I have a SendMessage() function that tries to write to the wire. I am trying to design the call so that it moves to a producer/consumer model if a lot of concurrent requests happen, but at the same time, stays single-threaded if there are no concurrent requests (for maximum performance).

I’m struggling on how to design this without race conditions because there is no way to move locks between threads.

What I have so far is something like (pseudo-coded):

SendMessage(msg) {

    if(Monitor.TryEnter(wirelock,200)) {

       try{
          sendBytes(msg);
       }
       finally {
          Monitor.Exit...
       }
   }
   else {

     _SomeThreadSafeQueue.add(msg)

     Monitor.TryEnter(consumerlock,..

          Task.Factory.New(ConsumerThreadMethod....
    }
}


ConsumerThreadMethod() {

   lock (wirelock) {

         while(therearemessagesinthequeue)

               sendBytes...

   }

}

Any obvious race conditions?

EDIT: Found a flaw in the last one. How about this instead?

SendMessage(msg) {

    if(Monitor.TryEnter(wirelock)) {

       try{
          sendBytes(msg);
       }
       finally {
          Monitor.Exit...
       }
   }
   else {

         _SomeThreadSafeQueue.add(msg)

        if (Interlocked.Increment(ref _threadcounter) == 1)
        {
            Task.Factory.StartNew(() => ConsumerThreadMethod());

        }
        else
        {
            Interlocked.Decrement(ref _threadcounter);

        }      
    }
}


ConsumerThreadMethod() {

     while(therearemessagesinthequeue)
           lock (wirelock) {
               sendBytes...
            }
      }

     Interlocked.Decrement(ref _threadcounter);
}

So basically using the interlocked counter as a way to only ever spawn one thread (if necessary)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T12:20:05+00:00Added an answer on May 29, 2026 at 12:20 pm

    No obvious races but TryEnter is a cause for some serious idle time. I actually think that using a consumer thread all the time is the best solution. If there is little to do, the overhead will be really small (the consumer thread will be asleep when not working, if designed correctly).

    Now you create a new task for each sent message, resulting in huge contention on the lock, since you are using a while loop in the consumer thread.

    EDIT: Since you are using non-blocking sockets, a single consumer thread should be enough to handle all send requests. The throughput of a single thread is higher than your network. If you have more consumers it’s hard to make sure that no two consumer threads send on the same socket, without serializing everything using a mutex. I don’t think switching between single-threaded and multi-threaded is a good idea.

    Your current “multithreaded” solution does not give you any performance gain since all work is protected using the same mutex. It will be as slow, or slower, than a single thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hoping someone can help with this. I have other VM's that dont have problems,
Hoping someone can help me out with this. Lets say I have a table
I've got an app design question that I'm hoping someone can help with. Let's
I am hoping someone can help me with this. What I have been looking
I'm hoping someone can help me with this. I have a MS SQL table
I'm hoping someone can help out with this. I modified code from jQuery and
I am hoping someone can help interpret the last code line (@Active=1....) for this
im hoping someone can help me quickly with a question i have. As site
I am hoping someone can help me with a question i have relating to
I'm hoping someone can help me as I've been stuck on this problem for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.