Results 1 to 4 of 4

Thread: Threads and lockers, mutexs question

  1. #1
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Threads and lockers, mutexs question

    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.

    Qt Code:
    1. void Thread::run()
    2. {
    3. assert(textEdit != 0);
    4. assert(tree != 0);
    5.  
    6. QTextDocument *document = textEdit->document();
    7. QTextCursor cursor(document);
    8.  
    9. cursor.movePosition(QTextCursor::Start);
    10.  
    11. while (true)
    12. {
    13. if (!cursor.movePosition(QTextCursor::NextWord, QTextCursor::KeepAnchor))
    14. break;
    15.  
    16. if (cursor.hasSelection())
    17. {
    18. QString word = cursor.selectedText();
    19. tree->insert(word); // *tree is going to be modified.
    20. }
    21. cursor.setPosition(cursor.selectionEnd());
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    May 2010
    Posts
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11
    Wiki edits
    11

    Default Re: Threads and lockers, mutexs question

    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.

  3. The following user says thank you to bcastalia for this useful post:

    Fox196 (24th August 2011)

  4. #3
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Threads and lockers, mutexs question

    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.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Threads and lockers, mutexs question

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Qt Threads vs Native Threads performance
    By StackOverflow in forum Qt Programming
    Replies: 1
    Last Post: 14th November 2010, 12:14
  2. Threads in Qt
    By freekill in forum Qt Programming
    Replies: 4
    Last Post: 11th November 2009, 18:49
  3. Threads...
    By Abc in forum Qt Programming
    Replies: 1
    Last Post: 19th June 2008, 17:35
  4. Replies: 2
    Last Post: 21st February 2008, 22:35
  5. question about socket and threads?
    By oob2 in forum Qt Programming
    Replies: 2
    Last Post: 27th February 2007, 11:42

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.