View Poll Results: Should I use a thread to wait on events?

Voters
6. You may not vote on this poll
  • Use a thread

    2 33.33%
  • Just poll the events using a function call from main()

    2 33.33%
  • Other (specify below)

    2 33.33%

Thread: Event-notified asynchronous Winsock methods

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    Event-notified asynchronous Winsock methods

    Ok, after several long distractions (games, other projects, vacation), I'm finally back to writing my asynchronous winsock wrapper. Originally, I had a thread waiting on the events and extracting the network notifications to a queue for later processing by main() (one call to 'execQueuedEvents()' executes preset callbacks set by the user). However, coming back with a fresh mind, it has suddenly occurred to me that I could just have a function that polls the events from main() ('while(WaitForMultipleObjects..)'). By my reasoning, this would eliminate the need for a separate thread - and therefore the thread-synchronization problems associated with it. After doing a profiling test I determined that WaitForMultipleObjects does not cause any significant delay when used for polling either; so I'm in a dilemma: Should I redesign the wrapper without the thread, or is there some advantage to using one? I currently see no reason not to get rid of it, but all the sites I've seen use a thread to handle the network events.

    Does anyone have any comments or suggestions about this?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    Registered User
    Join Date
    Jun 2003
    Location
    Austria
    Posts
    55
    for windowed apps i really like this method:
    http://msdn.microsoft.com/library/de...ncselect_2.asp

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Yes, it is convenient. But window messages are slow, and (of course) require a window. I am trying to implement a wrapper using WSAEventSelect() instead of AsyncSelect() so that it can be used with any and all of my applications, running at a reasonable speed so that it can also be used for realtime multiplayer games.

    **P.S.
    Also, if an asynchronous call to send() completes successfully (not WSAEWOULDBLOCK), does that mean that the buffer can then be safely deleted?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ok, well after reading more about how FD_WRITE behaves, I've decided that I'm going to try writing my own output buffer code that automatically completes send requests once the socket is writable, even if send() initially gives a wouldblock error... and it seems to me that the most efficient method would be to do so in a separate thread, even with all the thread synchronization headaches. I suppose it'll be a learning experience

    I'd still like to hear any comments or suggestions from people though.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    All you need are two things:

    1) a good (sync) socket wrapper.
    2) a good thread wrapper.

    from those simple building blocks you can create powerful sync/async networking objects without even bothering with MS's nutty API.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>1) a good (sync) socket wrapper.
    I wrote a mediocre blocking socket wrapper.

    >>2) a good thread wrapper.
    I wrote one. No idea how good it is though.

    from those simple building blocks you can create powerful sync/async networking objects without even bothering with MS's nutty API.
    Are you speaking of blocking using select() inside a separate thread? If so, how do you signal that the program should close? And if you're speaking of polling using select(), isn't that really inefficient? And if neither of the above, then what are you speaking of? lol
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    what I did was take a (blocking) socket class I wrote and derived a class from it that maintained a linked list of thread objects. all of the blocking calls were then overridden to launch a thread that would monitor and timeout the operation. besides this I always run any networking code from it's own thread to simplify things. you may find it easier to go about it some other way - that's just how I felt like doing it at the time and it worked rather well for me.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>all of the blocking calls were then overridden to launch a thread that would monitor and timeout the operation.
    Wow, didn't that give you a huge performance slowdown? I read somewhere that if you have too many threads going on then you lose a lot of CPU cycles to context switches, and also it takes time to create a thread. That didn't give you any problems??
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Wow, didn't that give you a huge performance slowdown?

    as long as the threads are already created (in suspended mode), there isn't much of a performance penalty. when the thread completes, you start it again in suspended mode till the next operation.

    >> I read somewhere that if you have too many threads going on then you lose a lot of CPU cycles to context switches

    that's going to depend on two things - how many threads you have running at one time, and the priority of each thread. the implementation I used didn't require too many threads, and I kept their priority levels relatively low.

    going back to your original question I'd say stick with your current method, except maybe put your WaitForMultipleObjects() routine in it's own thread so that you can use main() for other things (the user interface, for instance).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>going back to your original question I'd say stick with your current method, except maybe put your WaitForMultipleObjects() routine in it's own thread
    By 'current method' are you referring to the 'thread waiting on events and extracting notifications to a queue for later processing by main()' method, or the 'forget the thread and just use WaitForMultipleObjects with a timeout of 0 in main()'? I was sort of hoping I could do away with the thread without killing performance/functionality.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Winsock asynchronous socket + MessageBox() = trouble?
    By jmd15 in forum Networking/Device Communication
    Replies: 8
    Last Post: 02-07-2006, 02:50 PM
  3. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  4. delegate & event
    By Micko in forum C# Programming
    Replies: 5
    Last Post: 03-08-2004, 04:05 AM