Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

C threading question: accept() being called twice

  • 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, Registered Users 2 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, Registered Users 2 Posts: 2,062 ✭✭✭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