Threads vs async events

This is a discussion on Threads vs async events within the Tech Board forums, part of the Community Boards category; G'day, Not strictly related to C programming, hence my post here. I'm currently writing a simple proxy server. Basically with ...

  1. #1
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459

    Threads vs async events

    G'day,

    Not strictly related to C programming, hence my post here. I'm currently writing a simple proxy server. Basically with my model so far, I spawn a thread per connection (there is a user-defined limit). However, I don't think that would scale too well. Plus most OSes I target only support ~100 threads per process.

    At the moment all my proxy server does is read and forward data to another socket. So my question is, would I see an advantage switching to a library such as libevent? It should make thread issues disappear, and if I needed to work with the data I could always create a worker thread.

    Just trying to get some feedback whether switching to an event driven loop with libevent is a better way to go.

    If it matters, I'm targeting both Linux and Windows. Research shows MySQL has a connection to thread ratio of 1:1 (same as my program). However my requests would most likely take much longer to serve than MySQL requests.

    Thanks,
    Zac
    Last edited by zacs7; 02-10-2010 at 02:03 AM.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,439
    I believe it's a good choice Zacs if indeed it fits your bill to change the concept to event-driven. However, you can achieve similar (or better) results by adopting not a thread-per-connection setting, but instead setting up a thread pool.

    As far as I understand libevent, you will have a problems if later you do indeed want to add threads. libevent does not really have thread-safe dispatch and queuing. It's essential a single-threaded framework. They keep working on adding thread-safe features, but last time I checked it's still full of problems.

    So you will be stuck to libevent. As far as scalability goes this may not be what you want. And you know that it will not be that easy to change your code from an event-based model back to a thread-based one, if you later regret the decision.

    But if you feel that libevent will suit your scalability requirements, why not? It's a very good library (heck, it even supports buffered events! That's sweet.). Otherwise the norm for scalability still is Thread Pools.

    EDIT: A thought occurred just now to me from this quote "At the moment all my proxy server does is read and forward data to another socket". If by any chance you need to preserve order of execution, you can indeed apply libevent to a thread pool, I think. I see no problem with that. You will need to pipe libevent to one thread. Essentially call event_init() in one thread and keep it that way. I think there may be other issues, but all will be solvable, because essentially what you will be doing is proxying the Thread Pool with libevent. Meanwhile you may also want to check libev
    Last edited by Mario F.; 02-10-2010 at 06:17 AM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The apache solution to this is to use multiple processes with multiple threads.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,439
    That's through the MPM module and its in fact a pooling mechanism too. It's not a bad idea, but I doubt Zacs requirements demand such a killer.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Thanks for the input Mario, really helpful.

    I think I will stick with threads, since it seems to work fine. And being a small, home or personal proxy the huge scalability isn't an issue as such. It wouldn't be too hard to switch to thread-pooling later if required too, if I stick with threads. That's good to know about apache, definitely in a different ball park.

    Thanks again to both of you .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help: Multi-threading and Synchronization
    By Anom in forum C Programming
    Replies: 7
    Last Post: 12-08-2009, 04:34 PM
  2. Replies: 5
    Last Post: 10-17-2008, 11:28 AM
  3. problem with win32 threads
    By pdmarshall in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2004, 02:39 PM
  4. Console App w/ Threads and Events?
    By sean in forum C# Programming
    Replies: 1
    Last Post: 07-02-2004, 12:16 AM
  5. Block and wake up certain threads
    By Spark in forum C Programming
    Replies: 9
    Last Post: 06-01-2002, 03:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21