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

  • Home
  • SEARCH
  • 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 7591759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:43:51+00:00 2026-05-30T20:43:51+00:00

The application I am currently working on is a server which will manage connection

  • 0

The application I am currently working on is a server which will manage connection with the client using select(), each time the server receive a message, it will open a new thread in order to read the socket. During that time the file descriptor of the socket is remove from the set, and will be added at the end of the read.
Here is a sample of the code

struct s_handle {
 int sock;
 fd_set * rdfs;
};




int main(){
 ...
 fd_set rdfs;
 ...
 while(1){
 ....
  select(nb_fd,&rdfs,NULL,NULL,NULL)
  for_each(peer){
   if(FD_ISSET(peer->sock,&rdfs)){
     struct s_handle * h = malloc(sizeof(struct s_handle));
     h->sock = peer->sock;
     h->rdfs = &rdfs;
     FD_CLR(peer->sock,&rdfs);
     pthread_create(thread,NULL,handle,(void *)&h);
   }
  }
 ...
 }
 ...
 }

void* handle(void* argss){
 struct s_handle * temp = (struct s_handle *) argss;
 ...
 FD_SET(temp->sock,temp->rdfs);
}

Does FD_SET, FD_ISSET and FD_CLR are atomic operation, or do i need to lock rdfs with a mutex?

If a mutex is needed, how can I avoid deadlocks?

  • 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-30T20:43:53+00:00Added an answer on May 30, 2026 at 8:43 pm

    First, you shouldn’t be creating threads like that. Creating a thread is a fairly high overhead operation and should only be used when you need more threads, not just because you have to do more work.

    And yes, you do need to protect the FD_* functions with a mutex. The usual solution is to have a mutex that you hold only for the split second it takes to perform the FD_* operation. Before calling select, you acquire the mutex, make a copy of the descriptor set, and then release the mutex.

    In general though, it’s a bad idea to remove the socket from the read set. Putting the socket back in the read set won’t change the select that’s already occurring later on. And you’ll have the ugly mess of figuring out how to get the thread calling select out of select in order to operate on the new set.

    You may want to rethink your I/O discovery method and use one of the standard ones instead of trying to roll your own. You’re forcing the ugly tradeoff of either having some sockets not being listened to for read because the were recently read and the select is still blocked or having to re-select as you finish reading from each socket. Neither solution is good.

    A more common pattern is to keep the socket in the set while you’re reading on it and not go back to select until all sockets have been read from (but not necessarily had their data processed).

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

Sidebar

Related Questions

Currently I'm working on a Server-Client system which will be the backbone of my
I'm currently working on a project which needs some server-client communication. We're planning to
I am working on an application (using the spring framework) in which current time
I'm currently working on a Silverlight application connected to a SQL Server 2008 Express
I'm currently working on migrating an application from MS-Access to MS SQL Server. In
We are currently working on an application that will use a WCF service. The
I am new to android development. Currently i am working on an application which
We're working on some Ruby on Rails web application which is currently in English
I am currently working on one android application which has C2DM implementation and I
I'm currently working to specify my company's new partner/public API, which will be a

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.