PDA

View Full Version : Sub-Threading



TheGrimace
6th June 2007, 18:00
Is there any method other than exec to ensure that a thread's event loop will get processor time?

My issue is this:

I am starting a thread in the run method of another, but that thread doesn't seem to get processed. Right now I am use a while loop with msleep(10) for blocking.

Bitto
6th June 2007, 18:48
You have no way of _guaranteeing_ any thread execution time; that only works in real-time operating systems, and I'm going to assume you're not running one ;-).

If you can show some code, maybe it's easier to see what's going on. The only two things that pop to my mind are that both threads are running, but one is blocked somehow, or that only one thread is running. Maybe you're passing LowestPriority and the OS isn't scheduling the thread, but that's very unlikely.

So, code please :-).

TheGrimace
7th June 2007, 14:24
I worked it out with a co-worker. It turns out the following was happening:

Main thread started sub-thread.

Main thread called stop on thread after receiving a specific signal. (In this case it was looking for a specific value)

My main thread called start on it again a couple function calls later.

The problem was: The main thread never checked to make sure the sub-thread was stopped before moving on. So I got into situations where I was trying to start a thread that was still in the process of stopping.

I solved it with a simple while loop after the stop commands:


while(subThread->isRunning())
{
msleep(1);
}

jpn
7th June 2007, 14:32
Have you noticed QThread::wait()?

TheGrimace
7th June 2007, 16:38
Yes, but for our program we had to overload the isRunning function call. QWait would work for anyone else with this problem, though.