Results 1 to 11 of 11

Thread: QSocketNotifier: socket notifiers cannot be disabled from another thread

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSocketNotifier: socket notifiers cannot be disabled from another thread

    Quote Originally Posted by danadam View Post
    Qt::AutoConnection (slot is executed in main thread):
    That's quite interesting. The docs say:
    If the signal is emitted from the thread in which the receiving object lives, the slot is invoked directly, as with Qt::DirectConnection; otherwise the signal is queued, as with Qt::QueuedConnection.
    So it looks like that if you use Qt::AutoConnection, the effective connection type is established for each signal, not once when the connection is created.

    Quote Originally Posted by danadam View Post
    Qt::QueuedConnection (slot is executed in main thread):
    It looks like the Client object lives in the main thread, so the meta call events go through main thread's event queue. One possible solution is to use QObject::moveToThread() (of course in such case there must be an event loop running in the Client thread).

  2. #2
    Join Date
    Mar 2007
    Posts
    5
    Qt products
    Qt4

    Default Re: QSocketNotifier: socket notifiers cannot be disabled from another thread

    Quote Originally Posted by jacek View Post
    That's quite interesting. The docs say:
    So it looks like that if you use Qt::AutoConnection, the effective connection type is established for each signal, not once when the connection is created.


    It looks like the Client object lives in the main thread, so the meta call events go through main thread's event queue. One possible solution is to use QObject::moveToThread() (of course in such case there must be an event loop running in the Client thread).


    I solve the problem as below, but there is other things to solve.

    Sender thread lives in main thread.
    Client lives main thread.
    Socket lives in Client thread.

    As you have said , with direct connection between sender and client ,Client slot is invoked in sender thread. That is why, slot can not write to socket due to fact that socket lives in Client thread.

    With queued connection between sender and client , slot of Client is invoked in main thread because client lives in main thread. That is why, client slot can not write socket due to fact that socket lives in Client thread not in main thread.

    To solve the problem, I have created new class that is responsible for initialization of the socket. This class also have slot that writes to the socket. I have created this class in the run block of the Client class. And connect to signal of the sender with slot of this class. it worked correctly using queud connection but as you said I needed "exec" call to be able use queued connection.

    But when I call "exec" it blocks in the run block. I can not do anything inside. for example I can not use blocking approach as follows.
    Qt Code:
    1. while (!quit)
    2. {
    3.  
    4. if (tcpSocket->waitForReadyRead(Timeout))
    5. {
    6. // Read data
    7. QDataStream in(tcpSocket);
    8. in.setVersion(QDataStream::Qt_4_0);
    9. in >> buffer;
    10. qDebug() << buffer<<endl;
    11.  
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    because I have to read and write same socket in my design I have to alternative

    1 -) Do not use blocking approach. Use simply signal / slot way. but as I know signals are slow . my application time critical. should handle lots of messages.I can not choose this approach.

    2-) use blocking approach but create separe sockets for writing and reading.

    3-) Use an other socket library

    am I right ?

  3. #3
    Join Date
    Aug 2006
    Location
    Switzerland
    Posts
    52
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSocketNotifier: socket notifiers cannot be disabled from another thread

    Quote Originally Posted by dabiabilus View Post
    As you have said , with direct connection between sender and client ,Client slot is invoked in sender thread. That is why, slot can not write to socket due to fact that socket lives in Client thread.

    With queued connection between sender and client , slot of Client is invoked in main thread because client lives in main thread. That is why, client slot can not write socket due to fact that socket lives in Client thread not in main thread.
    To say the truth I don't know why you can't write to socket in different thread. My previous guessing was only... er... guessing. I checked that and it appears that there is no problem with writing to socket in sender thread or main thread. Client slot in the above examples did it and it wasn't complaining.
    The Wheel weaves as the Wheel wills.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSocketNotifier: socket notifiers cannot be disabled from another thread

    Quote Originally Posted by dabiabilus View Post
    1 -) Do not use blocking approach. Use simply signal / slot way. but as I know signals are slow
    What exactly is slow in signals?

    Quote Originally Posted by dabiabilus View Post
    am I right ?
    There is also a 4th solution: you can use a non-blocking approach without using signals and slots.

Similar Threads

  1. question about socket and threads?
    By oob2 in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2007, 11:42
  2. Problem closing a QMainWindow in Qt4.2
    By ian in forum Qt Programming
    Replies: 11
    Last Post: 17th October 2006, 00:49
  3. How to write on a socket in another thread?
    By Valheru in forum Qt Programming
    Replies: 7
    Last Post: 12th October 2006, 10:52

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
  •  
Qt is a trademark of The Qt Company.