Does QThread::sleep() require the event loop to be running?

Posted by suszterpatt on Stack Overflow See other posts from Stack Overflow or by suszterpatt
Published on 2010-04-03T11:01:53Z Indexed on 2010/04/03 11:33 UTC
Read the original article Hit count: 285

Filed under:
|
|
|

I have a simple client-server program written in Qt, where processes communicate using MPI. The basic design I'm trying to implement is the following:

  1. The first process (the "server") launches a GUI (derived from QMainWindow), which listens for messages from the clients (using repeat fire QTimers and asynchronous MPI receive calls), updates the GUI depending on what messages it receives, and sends a reply to every message.

  2. Every other process (the "clients") runs in an infinite loop, and all they are intended to do is send a message to the server process, receive the reply, go to sleep for a while, then wake up and repeat. Every process instantiates a single object derived from QThread, and calls its start() method. The run() method of these classes all look like this:

from foo.cpp:

void Foo::run()
{
    while (true)
    {
        // Send message to the first process
        // Wait for a reply
        // Do uninteresting stuff with the reply
        sleep(3);    // also tried QThread::sleep(3)
    }
}

In the client's code, there is no call to exec() anywhere, so no event loop should start.

The problem is that the clients never wake up from sleeping (if I surround the sleep() call with two writes to a log file, only the first one is executed, control never reaches the second). Is this because I didn't start the event loop? And if so, what is the simplest way to achieve the desired functionality?

© Stack Overflow or respective owner

Related posts about c++

Related posts about qt