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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:19:56+00:00 2026-05-27T10:19:56+00:00

I have a situation where a thread is selecting on sockets that are managed

  • 0

I have a situation where a thread is selecting on sockets that are managed by a separate thread.

When a socket is closed, select() presumably returns that socket as “available”, and it isn’t until I try to read from it that I realize that it is closed.

But I’m seeing a paradox: when the socket is closed from the other thread, the system is free to reallocate its file descriptor for other purposes. (I think.)

How can I be guaranteed that by the time I read from the socket (just a numeric descriptor) the system hasn’t already recycled that descriptor and used it for a new socket? In other words, for all I know I might be reading from some other socket that has been recently opened (perhaps a socket I shouldn’t even be including in my select()!) instead of the just-closed socket.

I could keep a list of recently closed descriptors, but I’m wondering if there is a better way.

  • 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-27T10:19:56+00:00Added an answer on May 27, 2026 at 10:19 am

    The short answer: Don’t close sockets from another thread, that you’re reading from in this one!

    The FD could, potentially, be reassigned. But you will have problems if you’re reading from one FD in multiple threads without some kind of scheme to communicate between them. Now, if you have a “Socket description” structure in shared memory that has a control semaphore and some indications of the FD, and other status information, maybe that could be manageable, but I think you’ll discover that the simplest solution is almost always to make FD’s specific to a single thread…

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

Sidebar

Related Questions

Here's the situation, I have a thread running that is partially controlled by code
Let's say I have a situation in Silverlight where there is a background thread
I have situation, where running a query that filters by an indexed column in
When you have a situation where Thread A reads some global variable and Thread
I have a situation where I would like to have the main thread waiting,
Here is the situation: I have to create a program that would input another
I have this situation that is really irritating me now. At random times with
I have a situation where thread 1 is waiting on a condition variable A,
I have a situation where I expect adding to the thread pool to be
I have a situation in Java where I have an external device that I

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.