Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C threading question: accept() being called twice

Options
  • 17-10-2013 10:38pm
    #1
    Closed Accounts Posts: 3,981 ✭✭✭


    The following code is being called twice, *sometmies*, when I make a single request. Does anyone have any idea why?

    The numbers are line numbers. Code is here:
    while(1)
    {
              if ( (connfd = accept(listenfd, (struct sockaddr*)NULL, NULL) ) >=0) {
                  pthread_t thread;
                  pthread_create(&thread, NULL, (void *) processConnection, (void*) &connfd);
                  pthread_join(thread, NULL);
    }
    


Comments

  • Closed Accounts Posts: 3,981 ✭✭✭[-0-]


    >= did it. Heh. Strange thing is before I added threading that wasn't there. Damn these fat fingers!


  • Registered Users Posts: 434 ✭✭TheBoffin


    Easy do at this time of night. Having an argument with a listview and friend values at the moment - Coffee Time!


  • Registered Users Posts: 2,021 ✭✭✭Colonel Panic


    I'm guessing some kind of server? I hope you don't mind some unsolicited advice for the future. Don't spawn a thread per request, create a pool of threads in advance, and when a request comes in, let the next free thread handle it.


  • Closed Accounts Posts: 3,981 ✭✭✭[-0-]


    I'm guessing some kind of server? I hope you don't mind some unsolicited advice for the future. Don't spawn a thread per request, create a pool of threads in advance, and when a request comes in, let the next free thread handle it.

    Interesting. I was thinking of using the max threads allowed for a process, but I want this server to be very light weight so I'll probably limit the number of threads to the number of concurrent connections I allow, which right now is only 100. I'll create a threadpool wrapper for pthread. Cheers!


Advertisement