Qt-interest Archive, February 2008
Thread synchronization problem
Message 1 in thread
Since my original post for some reason was deleted, I post it again.
-----Original Message-----
From: Malyushytsky, Alex
Sent: Friday, February 22, 2008 1:45 PM
To: 'qt-interest@xxxxxxxxxxxxx'
Subject: Threading synchronization problem
Hi all,
I got the Gui preparing the list of script commands, and need them to be
parsed and executed one by one in the already written code. Due to this
code design the only place I can add my code at is the place where the
single command is read (code is written for command line execution and
loops itself, until the specific command is issued).
So I tried to put this code in additional thread and tried to
synchronize the threads so, when main thread needs to execute the list
of commands, it loops though them and executes a single command in the
following way.
When the command is ready main thread wakes up working thread and waits
until working thread wakes it up.
When working thread is done with this command and is ready to execute
another one, it wakes up the main thread and puts asleep itself.
It looked like that the following implementation would do the job.
QWaitCondition FlexTread::condition;
QWaitCondition FlexTread::mainWaitCondition;
QWaitCondition* FlexTread::getWaitCondition()
{
return &condition;
}
QWaitCondition* FlexTread::getMainWaitCondition()
{
return &mainWaitCondition;
}
// called from main thread when command is ready
void FlexTread::waitForCommandExecuted()
{
FlexTread::getFlexThread()->getWaitCondition()->wakeAll();
FlexTread::getFlexThread()->getMainWaitCondition()->wait();
}
// called from working thread when ready to parse the command
void FlexTread::waitForCommand()
{
FlexTread::getFlexThread()->getMainWaitCondition()->wakeAll();
FlexTread::getFlexThread()->getWaitCondition()->wait();
}
Unfortunately this does not work and leads to the dead locks, due to the
fact that when the thread is woke up by condition it can start executing
immediately and the following order of the operator execution is
possible (and happens):.
// 1. from FlexTread::waitForCommand()
FlexTread::getFlexThread()->getMainWaitCondition()->wakeAll();
// 2. from FlexTread::waitForCommandExecuted()
FlexTread::getFlexThread()->getWaitCondition()->wakeAll();
// 3. from FlexTread::waitForCommandExecuted()
FlexTread::getFlexThread()->getMainWaitCondition()->wait();;
// 4. from FlexTread::waitForCommand()
FlexTread::getFlexThread()->getMainWaitCondition()->wait();
Every thread is waiting for conditions.
I tried to play with sleep(1) right after wait() so thread tries to give
other thread time to finish, but could not really make it work.
The only solution I can think about is to create another thread which
will be actually waking up 2 others in a specific order. But I don't
really like the idea having even more threads.
I would prefer also a solution based on QT 3, since my application is
cross platform.
The problem I am having should be more or less common, so someone might
see something I am missing.
Any ideas?
Thanks in advance,
Alex Malyushytsky
Research Engineer - Weidlinger Associates Inc.
office: 650 230 0210,
direct: 650 230 0349
web: http://www.wai.com
--
[ signature omitted ]