PDA

View Full Version : Thread Ownership Problem



tntcoda
8th June 2009, 20:11
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?



mythread::run()
{
nsock = new nsock();
...
// create a QTimer
exec();
}


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

wysota
9th June 2009, 01:18
Could we see the implementation of nsock? And a more extensive piece of the run() method, please :)