views:

294

answers:

1

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?

A: 

Some classes in client code may need an event loop to be started. Why to use QThreads for clients if you don't have an event loop for the clients and you already using MPI?

zoli2k
The only reason I'm deriving from `QThread` in the clients is because it gives me easy access to sleep functionality via `QThread::sleep()`. I'd like to avoid busy waiting.
suszterpatt
exec is not necessarily bussy wait, from my wathcing fo CPOU comsumption it is not, there is something more smart behind (or your CPU will actually take all time 100%)so go for exec and signal slot concept
penguinpower
I know `exec()` isn't busy waiting, I meant that I'd like to avoid waiting by, for example, starting a timer and then putting `while (timer.isActive()) {}` in the code. `QThread::sleep()` is the most convenient solution I could find.
suszterpatt