PDA

View Full Version : signal/slot accross different threads(different subclasses QThread)



milli
24th June 2011, 20:08
I want to emit a signal from a thread and the slot to be executed on another thread(of different subclass QThread).
For example,
i have these subclasses QThread: ServerThread and ClientThread.
I want to emit signal from ClientThread and in ServerThread class a slot to be executed.
The problem is,when multiple ClientThreads* threads emit signals then slots from ServerThreads* threads are executed only for the first signal that emitted.
Why this occurs?
If am not clear,tell me to post sample code.
Thanks!

stampede
24th June 2011, 21:41
Yes, post your code, it'll be easier to spot the problem, now we can only guess.

milli
24th June 2011, 22:13
void MainServer::newConnectionFromAdmin() {


QTcpSocket * tcpSocket = tcpServer.nextPendingConnection();
ServerThread *serverThread = new ServerThread(tcpSocket->socketDescriptor(),this);
serverThread->start();
connect(serverThread,SIGNAL(finished()),serverThre ad,SLOT(deleteLater()));
}



void MainServer::newConnectionFromClient()
{

QTcpSocket * tcpSocket = tspServer.nextPendingConnection();
ClientThread *clientThread = new ClientThread(tcpSocket->socketDescriptor(),this);
clientThread->start();
connect(clientThread,SIGNAL(finished()),clientThre ad,SLOT(deleteLater()));
}

ServerThread::ServerThread(int num, QObject *parent)
: QThread(parent), num(num)
{

tcpSocket=new QTcpSocket(this);
tcpSocket->setSocketDescriptor(num);


//connections for sockets
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readReq()));
connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));




}
/* The running thread's running code */
void ManagerServerThread::run()
{
connect(MainServer *)(this->parent()),SIGNAL(reportToServer(TRep*)),this,SLOT( writeData(TRep*)));
exec();
}


ClientThread::ClientThread(int num, QObject *parent)
: QThread(parent), num(num)
{

tcpSocket.setSocketDescriptor(num);


//connections for sockets
connect(&tcpSocket, SIGNAL(readyRead()), this, SLOT(func()));
connect(&tcpSocket, SIGNAL(disconnected()), this, SLOT(connectionClosedByServer()));
connect(&tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),this, SLOT(displayError(QAbstractSocket::SocketError)));


connect(this,SIGNAL(reportToAdmin(TRep*)),(MainSer ver *)(this->parent()),SIGNAL(reportToServer(TRep*)));

// qDebug() << Q_FUNC_INFO << QThread::currentThreadId();

}
void ClientThread::run()
{
exec();
}
int ClientThread::func()
{
emit reportToAdmin(rep);
{

stampede
24th June 2011, 23:30
Short description of the code could be good to - what results do you expect, and what results you get ?

milli
24th June 2011, 23:58
So,when the signal reportToAdmin(rep) is emitted i want slot writedata (ServerThread class) to be executed.When one thread is running the the result is expecting.However when multiple threads emit signal reportToAdmin(rep) then slots from ServerThreads* threads are executed only for the first signal that is emitted.

Santosh Reddy
25th June 2011, 05:59
I guess you should be getting some warnings, saying connection not made or object not found... check the Application Output

One quick comment, you are creating ClientThread in the thread context where MainServer resides, and trying to emit a signal (which is of ClientThread's object) from some other thread, you need to move the ClientThread object to the context of thread which emit's reportToAdmin().

one quick try, if this is the only problem is to add moveToThread(clientThread); as last thing in void MainServer::newConnectionFromClient()

milli
25th June 2011, 10:16
No i am not getting errors.I added moveToThread(clientThread) but the result is the same..Could someone to propose me a good tutorial with examples for QThreads except Qt Documentation which i have read it.
Thanks!

DanH
25th June 2011, 10:24
My guess is that reportToAdmin isn't returning, but is waiting somewhere. A new signal won't be delivered until the receiving thread returns to its event loop.

Santosh Reddy
25th June 2011, 11:14
Could someone to propose me a good tutorial with examples for QThreads except Qt Documentation which i have read it.
Reading the documentation is not enough, you need to really understand the how QThread works, and to do so you need have a understand threads and multi-threading concepts (in addition to C, C++, Qt)

Please consider this reply as an encouragement to understand the things better, rather than just knowing / using them.