Results 1 to 4 of 4

Thread: Need help/suggestion with multithreadding and sockets

  1. #1
    Join Date
    Mar 2009
    Posts
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Question Need help/suggestion with multithreadding and sockets

    Hello,

    I'm trying to build a class MySocket which I think would be best if it starts a thread for incoming data and stores that data in a buffer which should be shared with the parent thread. All other functions are outside this worker thread.

    Basic example would be a ping function. This function sends a ping to the server and waits for an answer and timeouts if no answer is received. This answer is parsed from the buffer. So in short, incoming data should be feed to the buffer while this function waits (which could take 10 seconds over laggish GPRS).

    How should I build this?
    Or are there any better ideas to accomplish this?
    If main thread declares MySocket and connects to server, can worker threads use this same connection?

    // Floppe

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Need help/suggestion with multithreadding and sockets

    You don't need a worker thread to handle a socket. Qt uses sockets in asynchronous way - you will be notified when there is anything to read from the socket. Simply connect to appropriate signals and you're done.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2009
    Posts
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Need help/suggestion with multithreadding and sockets

    So I also tought. However, it seems like the notify will come in after a lengty function has finished. So the ping function did never got an answer or maybe I did need to run something like processEvents. I got it to work with threads though.

    Thanks
    // Floppe

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Need help/suggestion with multithreadding and sockets

    Quote Originally Posted by Floppe View Post
    So I also tought. However, it seems like the notify will come in after a lengty function has finished.
    Yes, that's correct. You need to let the event loop process events from time to time. You should do that anyway to prevent your GUI from freezing.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.