Results 1 to 8 of 8

Thread: How to write on a socket in another thread?

  1. #1
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to write on a socket in another thread?

    Qt doesn't seem to like you trying to write to a socket that is running in another thread I have created a thread, which creates a socket, connects it's signals to slots in the parent object, and connects to the server.
    However, since the parent object runs in the GUI thread, trying to write to the socket doesn't work. Qt spits out a warning that socket notifiers can't be enabled from another thread. How does one get around this?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to write on a socket in another thread?

    Are you creating the socket in constructor or something? You must create the QTcpSocket object in QThread::run() when the new thread is actually running.

    Qt Code:
    1. void MyThread::run()
    2. {
    3. QTcpSocket socket;
    4. ....
    5. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to write on a socket in another thread?

    Yes, I am creating the socket in run()...thats' what's confusing me I have connected the readData() signal from the socket to a slot in the class in which the socket is a member of. Using qDebug() I can see that the slot is called, and the it in turn indeed calls the function to actually parse the data. It all goes pearshaped on the line where the function being called tries to write a command to the socket Here's the code:
    Attached Files Attached Files

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to write on a socket in another thread?

    Like I just mentioned in another thread:
    The destructor of the QThread subclass is executed in the thread where the QThread object lives in (usually same thread where it was created in), not in the thread being executed in QThread::run().
    I'm not willing to start examining almost 400 lines of code throughly just for fun, but for what I can see, you are creating the socket in run() and then writing to it in the destructor. These two blocks of code become executed in different threads. I'm also suspecting you having the problem described in this thread.
    J-P Nurmi

  5. #5
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to write on a socket in another thread?

    Thanks, I've just about fixed the problem though, by running a while loop inside run() that checks a command buffer that other threads can write to. This solves the problem, but introduces a new one that when the thread is doing nothing (spinning it's wheels in the while-loop) it uses 100% of the processor So now I have to find a way of overcoming that problem.

  6. #6
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to write on a socket in another thread?

    Here is the code if you are interested. It's a bit of a hack, but it works rather well.
    Attached Files Attached Files

  7. #7
    Join Date
    Jan 2006
    Location
    La Spezia,Italy
    Posts
    77
    Thanks
    9
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to write on a socket in another thread?

    Hi, consider using a QWaitCondition for doing that thing.
    As wysota explained me in the last part of the thread:
    http://www.qtcentre.org/forum/f-qt-p...dows-2623.html
    I didn't went through your code, however, for what I have understood you could do something like this:

    Create an EventLoop and put the thread(s) inside the event loop.
    Let the loop be the interface between your threads by connecting to it the signals
    coming from the threads(sockets) you have.

    bye.

  8. #8
    Join Date
    Aug 2006
    Posts
    163
    Thanks
    12
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to write on a socket in another thread?

    Thanks, but there is really nothing for it to wait for. It just has to wait until a preset amount of time has passed without any activity( ie. user commands or server responses). The so-called "timeout" value. I think that the sleep(0) call that wysota mentioned will work well though.

    /edit : hmm, calling sleep(0) at the end of every iteration of my while-loop does nothing for the processor usage

Similar Threads

  1. qt socket question
    By bluesguy82 in forum Qt Programming
    Replies: 4
    Last Post: 29th August 2006, 15:42
  2. simple thread layout question
    By mhoover in forum Qt Programming
    Replies: 1
    Last Post: 12th August 2006, 11:02
  3. Replies: 11
    Last Post: 7th July 2006, 13:09
  4. [QT4] QThread and printing a QList<QPixmap>
    By KShots in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 21:44
  5. Replies: 2
    Last Post: 6th January 2006, 21:15

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.