Results 1 to 2 of 2

Thread: Wait for thread to complete before reading data?

  1. #1
    Join Date
    Apr 2011
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Wait for thread to complete before reading data?

    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:

    Qt Code:
    1. QList<QByteArray> myList;
    2.  
    3. QList<QByteArray> MainWindow::getData(QString cmd) {
    4. thread = new MyThread(cmd);
    5. thread->start();
    6. connect(thread, SIGNAL(dataReady(QList<QByteArray>), this, SLOT(readThreadData(QList<QByteArray>)));
    7. //Lock myList?
    8. return myList;
    9. }
    10.  
    11. void MainWindow::readThreadData(QList<QByteArray> tList) {
    12. myList = tList;
    13. //Unlock myList?
    14. }
    15.  
    16. void MainWindow::loadDataFromList(QString cmd) {
    17. getData(cmd);
    18. //Read from myList?
    19. }
    To copy to clipboard, switch view to plain text mode 

    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...


    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.

    Qt Code:
    1. runCommand = new CommandRunner();
    2. arrayList = runCommand->doRun(cmd);
    3. return arrayList;
    To copy to clipboard, switch view to plain text mode 

    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.
    Last edited by TheVirus; 26th April 2011 at 03:58.

  2. #2
    Join Date
    Apr 2011
    Posts
    11
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Wait for thread to complete before reading data?

    After getting some sleep, I've written a more thorough description of my problem and how I am working with the sockets. This is all pseudo code.

    SockA -> connect to local service.
    SockB -> listen for connection. (localhost:9999)
    SockC -> connect to remote service.
    SockA -> send 'START on localhost:9999'
    LSERVICE -> SockA 'OK'
    LSERVICE -> connect to SockB
    LSERVICE -> SockB 'INIT'
    SockB -> SockC 'LIST'
    SockC -> SockB '1234'
    SockB -> LSERVICE '1234' (looks up what 1234 is)
    LSERVICE -> SockA 'John'
    LSERVICE -> SockA 'Q' (could be completely ignored and go straight to the next line)
    LSERVICE -> SockA 'Public'
    ... more commands


    I have the following setup:

    lsServer QTcpServer (opens port 9999 on localhost)
    rsServer QTcpSocket (connects to a remote service)
    cmdRunnr QTcpSocket (connects to local service to issue commands)

    I need to communicate between the 3 sockets in varying ways. I do not have control of the 'local service' program. lsServer and rsServer are non-threaded classes that read and write to each other's sockets.
    For example:
    Qt Code:
    1. connect(rsServer, SIGNAL(readyRead()), this, SLOT(readFromRemote()));
    2.  
    3. readFromRemote() {
    4. QByteArray input;
    5. input = rsServer->read(4096);
    6. lsServer->write(input);
    7. }
    To copy to clipboard, switch view to plain text mode 

    rsServer passes data that it receives to lsServer.

    I use cmdRunnr to connect to a local service and use my program as a proxy of sorts to issue commands to a remote host that I'm directly connected to. The problem is, I need a way to wait until a variable is valid before continuing and I need a way to wait for input to return to the socket before continuing.

    For example:
    Qt Code:
    1. getValidList() {
    2. QString cmd = "LIST";
    3. QList<QByteArray> myList;
    4.  
    5. myList = getListFromRemote(cmd);
    6. //... do stuff with myList ...
    7. }
    8.  
    9. getListFromRemote(QString cmd) {
    10. cmdRunnr = new CommandRunner(cmd);
    11. cmdRunnr->start();
    12. //... Find a way to get cmdRunnr->runnerList (QList<QByteArray>) ...
    13. return runnerList;
    14. }
    15.  
    16. in CommandRunner:
    17.  
    18. CommandRunner::CommandRunner(QObject *parent, QString cmd) : QThread { //constructor
    19. runnerCmd = cmd;
    20. }
    21.  
    22. CommandRunner:run() {
    23. QByteArray input;
    24. mySocket = new QTcpSocket();
    25. mySocket.connectToHost(localhost, 4351);
    26.  
    27. //... do I connect()? ...
    28. mySocket->write(runnerCmd);
    29. mySocket->waitForReadyRead(5000);
    30. input = mySocket->read(4096);
    31. //... input should be OK ...
    32. mySocket->waitForReadyRead(5000);
    33. input = mySocket->read(4096);
    34. //... input might be 'John' followed by more information or simply end at that ...
    35. }
    To copy to clipboard, switch view to plain text mode 
    The only way I know the data is finished is when the socket disconnects. There are headers I can read to determine if it's DATA or REPLY. I'm in a conundrum and am unsure of how to tackle this problem.

Similar Threads

  1. Replies: 3
    Last Post: 5th January 2011, 22:55
  2. Wait in thread till QTcp socket have some thing to read
    By hasnain in forum Qt Programming
    Replies: 2
    Last Post: 14th September 2010, 12:46
  3. wait for method to complete?
    By jtdavidson in forum Newbie
    Replies: 1
    Last Post: 18th July 2010, 07:16
  4. Replies: 3
    Last Post: 19th January 2010, 20:26
  5. Replies: 6
    Last Post: 29th April 2009, 18:17

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.