TheVirus
26th April 2011, 03:58
I'm using a thread to read input from a socket (the reasoning for the thread is when I use socket->waitForReadyRead(-1), it pauses my entire program and prevents another socket from accepting/sending data, which causes it to wait forever). I want to set data with that thread, so I used a SIGNAL/SLOT. The question I have, how can I make sure the data is set before reading from it? I'm looking at mutex locks, but I'm unsure of how to do it.
Here's a basic idea of what I'm doing:
QList<QByteArray> myList;
QReadWriteLock lock;
QList<QByteArray> MainWindow::getData(QString cmd) {
thread = new MyThread(cmd);
thread->start();
connect(thread, SIGNAL(dataReady(QList<QByteArray>), this, SLOT(readThreadData(QList<QByteArray>)));
//Lock myList?
return myList;
}
void MainWindow::readThreadData(QList<QByteArray> tList) {
myList = tList;
//Unlock myList?
}
void MainWindow::loadDataFromList(QString cmd) {
getData(cmd);
//Read from myList?
}
I'm not sure if this is the right approach but I'm not sure how to proceed. I'm sending a command to a program and I wait for input on a socket. The problem is, the program responds to another socket first and then responds to my request.
SockA -> Program
Program -> SockB
SockB -> SOMEWANSOCKET
SOMEWANSOCKET -> SockB
SockB -> Program
Program -> SockA
SockA/SockB are internal to my software. Program is an application I'm communicating with and SOMEWANSOCKET is some server software over the WAN I am communicating with.
What I'm getting is:
SockA -> Program
Program -> SockB
... Can't do anything because SockA is in an infinite loop waiting for readyRead().
I never noticed this before because I was doing everything via localhost and noticed it when I introduced the WAN. I hope that makes sense, it does in my head... :crying:
Added after 49 minutes:
Just a side note. The way I had it before, I didn't use threads. I simply created an object from another class that handled the input/output of the socket.
runCommand = new CommandRunner();
arrayList = runCommand->doRun(cmd);
return arrayList;
So, if I can do this without needing threads, that's fine with me, but I think I'll have to since I have to make sure I receive all the data before trying to parse it. If I lower my waitForReadyRead() times, it still fails as the function returns before the data is finished being sent.
Here's a basic idea of what I'm doing:
QList<QByteArray> myList;
QReadWriteLock lock;
QList<QByteArray> MainWindow::getData(QString cmd) {
thread = new MyThread(cmd);
thread->start();
connect(thread, SIGNAL(dataReady(QList<QByteArray>), this, SLOT(readThreadData(QList<QByteArray>)));
//Lock myList?
return myList;
}
void MainWindow::readThreadData(QList<QByteArray> tList) {
myList = tList;
//Unlock myList?
}
void MainWindow::loadDataFromList(QString cmd) {
getData(cmd);
//Read from myList?
}
I'm not sure if this is the right approach but I'm not sure how to proceed. I'm sending a command to a program and I wait for input on a socket. The problem is, the program responds to another socket first and then responds to my request.
SockA -> Program
Program -> SockB
SockB -> SOMEWANSOCKET
SOMEWANSOCKET -> SockB
SockB -> Program
Program -> SockA
SockA/SockB are internal to my software. Program is an application I'm communicating with and SOMEWANSOCKET is some server software over the WAN I am communicating with.
What I'm getting is:
SockA -> Program
Program -> SockB
... Can't do anything because SockA is in an infinite loop waiting for readyRead().
I never noticed this before because I was doing everything via localhost and noticed it when I introduced the WAN. I hope that makes sense, it does in my head... :crying:
Added after 49 minutes:
Just a side note. The way I had it before, I didn't use threads. I simply created an object from another class that handled the input/output of the socket.
runCommand = new CommandRunner();
arrayList = runCommand->doRun(cmd);
return arrayList;
So, if I can do this without needing threads, that's fine with me, but I think I'll have to since I have to make sure I receive all the data before trying to parse it. If I lower my waitForReadyRead() times, it still fails as the function returns before the data is finished being sent.