PDA

View Full Version : Threads and lockers, mutexs question



Fox196
23rd August 2011, 19:34
Hi I have a question.

In the code, textEdit is a pointer of a class that inherits from QTextEdit class, and is owned by the main window, tree is a pointer of a Tree class I created, and is owned by a dialog.



void Thread::run()
{
assert(textEdit != 0);
assert(tree != 0);

QTextDocument *document = textEdit->document();
QTextCursor cursor(document);

cursor.movePosition(QTextCursor::Start);

while (true)
{
if (!cursor.movePosition(QTextCursor::NextWord, QTextCursor::KeepAnchor))
break;

if (cursor.hasSelection())
{
QString word = cursor.selectedText();
tree->insert(word); // *tree is going to be modified.
}
cursor.setPosition(cursor.selectionEnd());
}
}


When I open the dialog, I want the thread to start.

But I'm getting this error.

ASSERT: "qApp && qApp->thread() == QThread::currentThread"

Is this because I'm not using lockers or mutexs? How should I use them?.

Also, I want to receive a signal when run has finished.
Should I use terminated signal, finished signal or create my own signal?

Thanks

bcastalia
24th August 2011, 04:15
There seem to be parts missing in code you've posted. You've said the the tree thread (a subclass of QThread, presumably) is "owned" by a dialog, which I take to mean that the tree pointer variable is a data member of the
dialog. Somewhere in the dialog implementation, then, I would expect the tree pointer to be assigned the result of a new QThread, and then tree->start() is called. You code defines a run method of a Thread class; what is the Thread class? Where is the Tree::run implementation? Let's see more details ....

The use of QMutex, or other mutex classes, is not required to use a QThread. Use a mutex when mutual exclusion (the meaning of "mutex") is need when accessing more than one thread might access the same object at the same time and this is to be prevented by guarding the access point with a mutex.

When the run method of a QThread implementation returns QThread will emit its finished signal. The terminated signal is emitted if the thread is prematurely terminated with a call to QThread::terminate; but this is generally not recommended. If needed, provide a variable in the QThread subclass implementation that will indicate to the run method when it should return. This is case where access to the variable should be guarded by a mutex.

The Mandelbrot example in the Threading and Concurrent Programming Examples of the Qt documentation provides a good model to follow.

Fox196
24th August 2011, 06:50
Hi, sorry for the lack of details and my bad English.

Tree is not a subclass of QThread, it's a class I made, it's not even a QObject.
Thread is a subclass of QThread and it has as data members tree and textEdit.

I just want that when I open my dialog the GUI doesn't freeze while I'm inserting words in my tree.

I think that the problem is because I should use a kind of lock when moving the cursor, or somewhere.

wysota
24th August 2011, 08:27
Your code simply won't work, you can't use threads with Qt objects like that. Tell us what is the purpose of having threads here.