Thread: Way Around Threads

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912

    Way Around Threads

    Still on my server program...

    The original flow chart consisted of the following:



    • Setting up a connection and listen()ing for requests.

    • In a loop, accept()ing requests and sending them to a new thread for processing.
    I've spent the last couple of days researching my options for threading, and it seemed as though the best option would be to include a third-party library, which I really wanted to avoid, so I had another idea:


    • Setting up a connection and listen()ing for requests.

    • In a loop, accept()ing requests and processing them right there in the same thread.
    I would of course specify a much larger number of connections to be allowed to wait to be accepted to make up for having one thread of execution.

    My logic is that even though only one request could be processed at a time, it wouldn't affect performance because not having to share processor time with any other thread would make up for that. I also wouldn't have to worry about thread-safety with any resources that I use.

    I am of course, not only really tired, but also an idiot. Is my logic flawed? Do you have any advice? This solution just seems to good to be true.

    Thanks in advance,
    Sean.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This is common. Perhaps not for a web server or whatever it is you were doing, but having a single thread which does everything is. Most MUDs do this.
    Code:
    set up socket
    while not done
        select
        accept new connections
        read from those sockets which have stuff to read
        process info
        write to sockets which have stuff to be sent
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Cool - thanks.

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    the performance hit with one thread is that when you block for IO, every request is waiting. With multiple threads, one request can be processed while another is blocked. (note that IO can be anything from reading from a socket to reading from disk)

    Your solution is called Time Based Multiplexing and is a common approach to servers but will be noticably slower than a multithreaded solution in high traffic situations.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-17-2008, 11:28 AM
  2. Yet another n00b in pthreads ...
    By dimis in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2008, 12:43 AM
  3. Classes and Threads
    By Halloko in forum Windows Programming
    Replies: 9
    Last Post: 10-23-2005, 05:27 AM
  4. problem with win32 threads
    By pdmarshall in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2004, 02:39 PM
  5. Block and wake up certain threads
    By Spark in forum C Programming
    Replies: 9
    Last Post: 06-01-2002, 03:39 AM