Results 1 to 2 of 2

Thread: Thread Ownership Problem

  1. #1
    Join Date
    Jun 2008
    Location
    UK
    Posts
    35
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Thread Ownership Problem

    Hi,

    I'm having some problems with a QTCPSocket outputting the error QSocketNotifier: socket notifiers cannot be enabled from another thread.

    After some research it appears that this occurs because the socket is being used in a different thread than the one it was created in, this is not intentional.

    In my subclass of QThread, the run() method creates an object on the heap which does some socket operations, now I assumed creating this object here would allocate it to the newly started thread?

    Qt Code:
    1. mythread::run()
    2. {
    3. nsock = new nsock();
    4. ...
    5. // create a QTimer
    6. exec();
    7. }
    To copy to clipboard, switch view to plain text mode 

    Apparently nsock is instantiated on thread ID: 0x9298a0, when my QTimer object gets triggered, and calls things in the nsock/QTCPsocket class it's in thread ID: 0x6dd5c0.

    So even though I instantiate the nsock pointer in the thread's run() method, its under the parent thread? And hence causes clashes when I try to use it in the QTimer which executes/triggers under the correct (new) thread.

    Please could someone give me some idea how to instantiate objects so they are owned by the new thread and not the parent thread.

    Thanks very much,

    Jack

  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: Thread Ownership Problem

    Could we see the implementation of nsock? And a more extensive piece of the run() method, please
    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.


Similar Threads

  1. QTcpSocket, QTcpServer problem
    By frido in forum Qt Programming
    Replies: 3
    Last Post: 3rd April 2009, 23:16
  2. Thread Help
    By tntcoda in forum Qt Programming
    Replies: 1
    Last Post: 16th January 2009, 22:10
  3. Non-GUI thread
    By foxyproxy in forum Qt Programming
    Replies: 3
    Last Post: 11th April 2008, 13:18
  4. Problem building Qt4.1.0 with thread support on windows XP
    By pavithra in forum Installation and Deployment
    Replies: 1
    Last Post: 1st April 2006, 11:35
  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.