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 8146861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:08:59+00:00 2026-06-06T14:08:59+00:00

Ever since I discovered sockets, I’ve been using the nonblocking variants, since I didn’t

  • 0

Ever since I discovered sockets, I’ve been using the nonblocking variants, since I didn’t want to bother with learning about threading. Since then I’ve gathered a lot more experience with threading, and I’m starting to ask myself.. Why would you ever use it for sockets?

A big premise of threading seems to be that they only make sense if they get to work on their own set of data. Once you have two threads working on the same set of data, you will have situations such as:

if(!hashmap.hasKey("bar"))
{
  dostuff               // <-- meanwhile another thread inserts "bar" into hashmap
  hashmap[bar] = "foo"; // <-- our premise that the key didn't exist
                        //     (likely to avoid overwriting something) is now invalid
}

Now imagine hashmap to map remote IPs to passwords. You can see where I’m going. I mean, sure, the likelihood of such thread-interaction going wrong is pretty small, but it’s still existent, and to keep one’s program secure, you have to account for every eventuality. This will significantly increase the effort going into design, as compared to simple, single-threaded workflow.

I can completely see how threading is great for working on separate sets of data, or for programs that are explicitly optimized to use threading. But for the “general” case, where the programmer is only concerned with shipping a working and secure program, I can not find any reason to use threading over polling.

But seeing as the “separate thread” approach is extremely widespread, maybe I’m overlooking something. Enlighten me! 🙂

  • 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-06-06T14:09:02+00:00Added an answer on June 6, 2026 at 2:09 pm

    There are two common reasons for using threads with sockets, one good and one not-so-good:

    The good reason: Because your computer has more than one CPU core, and you want to make use of the additional cores. A single-threaded program can only use a single core, so with a heavy workload you’d have one core pinned at 100%, and the other cores sitting unused and going to waste.

    The not-so-good reason: You want to use blocking I/O to simplify your program’s logic — in particular, you want to avoid dealing with partial reads and partial writes, and keep each socket’s context/state on the stack of the thread it’s associated with. But you also want to be able to handle multiple clients at once, without slow client A causing an I/O call to block and hold off the handling of fast client B.

    The reason the second reason is not-so-good is that while having one thread per socket seems to simplify the program’s design, in practice it usually complicates it. It introduces the possibility of race conditions and deadlocks, and makes it difficult to safely access shared data (as you mentioned). Worse, if you stick with blocking I/O, it becomes very difficult to shut the program down cleanly (or in any other way effect a thread’s behavior from anywhere other than the thread’s socket), because the thread is typically blocked in an I/O call (possibly indefinitely) with no reliable way to wake it up. (Signals don’t work reliably in multithreaded programs, and going back to non-blocking I/O means you lose the simplified program structure you were hoping for)

    In short, I agree with cib — multithreaded servers can be problematic and therefore should generally be avoided unless you absolutely need to make use of multiple cores — and even then it might be better to use multiple processes rather than multiple threads, for safety’s sake.

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

Sidebar

Related Questions

Ever since I started programming this has been something I have been curious about.
Ever since I moved from VB6 to VB.NET somewhere in 2005, I've been using
This has been bugging me ever since I started learning Ruby five months ago:
Well I have been thinking about this for a while, ever since I was
Ever since the publication of Enterprise Integration Patterns people have been using the notation
I've been wondering this ever since I started using MS's control templates examples as
Ever since I started using .NET, I've just been creating Helper classes or Partial
Ever since learned about using event aggregators to publish messages instead of using events
This is just something that's been puzzling me ever since I read about iterators
I guess I have been using the Web Site model ever since .NET 2.0

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.