Results 1 to 6 of 6

Thread: Qthread mutex

  1. #1
    Join Date
    May 2009
    Posts
    38
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Question Qthread mutex

    Qt Code:
    1. private bool stopped;
    2. ......
    3.  
    4. void Thread::run()
    5. {
    6. forever {
    7. mutex.lock();
    8. if (stopped) {
    9. stopped = false;
    10. mutex.unlock();
    11. break;
    12. }
    13. mutex.unlock();
    14. cerr << qPrintable(messageStr);
    15. }
    16. cerr << endl;
    17. }
    To copy to clipboard, switch view to plain text mode 

    I don't understand why do we have to use mutex to lock the memeber of this class?
    when you create the object of this class and call start() to start the thread, this object is unique, there is only one "stopped" !! why do we have to lock it before to write to it?

  2. #2
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qthread mutex

    but how many threads try to change the value stopped concurrently .. we should provide a queue to access the stopped value using QMutex , QSemaphores ...

  3. #3
    Join Date
    May 2009
    Posts
    38
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: Qthread mutex

    Quote Originally Posted by wagmare View Post
    but how many threads try to change the value stopped concurrently .. we should provide a queue to access the stopped value using QMutex , QSemaphores ...
    this is only one thread to access stopped!!!!! this is memeber variables, one instance of this class will only one stopped!!! there is only one thread could access this variable, why mutex it????

  4. #4
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qthread mutex

    Quote Originally Posted by dognzhe View Post
    this is only one thread to access stopped!!!!! this is memeber variables, one instance of this class will only one stopped!!! there is only one thread could access this variable, why mutex it????
    then no need for mutex .. if there is only one call for the thread run at a time ... if more than one thread function is called at a time and try to access thread class variables then we can go for Synchronization classes: QMutex, QReadWriteLock, QSemaphore, and QWaitCondition.

    The QMutex class provides a means of protecting a variable or a piece of code so that only one thread can access it at a time

    see the example ... it will clear all your doubts ...
    QTDIR/example/network/threadedfortuneserver/

  5. #5
    Join Date
    May 2009
    Posts
    38
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qthread mutex

    i don't really understand the thread in class.

    if i have code like this

    Qt Code:
    1. ServerThread *thread = new ServerThread( descriptor, this );
    To copy to clipboard, switch view to plain text mode 

    so i create the instance of this thread class. in the class I have

    Qt Code:
    1. priave int ServerThread::num
    2.  
    3. run{
    4. num ++;
    5. }
    6.  
    7. thread->start();
    To copy to clipboard, switch view to plain text mode 

    now i create another thread.
    Qt Code:
    1. ServerThread *thread2 = new ServerThread( descriptor, this );
    To copy to clipboard, switch view to plain text mode 

    thread2->start();

    in this case. I think that i don't need to protect the variable int num. because they are in different instance, is that right?

  6. #6
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qthread mutex

    right ... only if u call the same thread instance concurrently and try to change its global variable u need protection ...

Similar Threads

  1. Why slots in QThread subclasses are unsafe?
    By AlphaWolf in forum Qt Programming
    Replies: 8
    Last Post: 30th May 2010, 15:39
  2. QThread and QTimer
    By sivrisinek in forum Qt Programming
    Replies: 4
    Last Post: 30th April 2009, 16:41
  3. QThread and QTcpSocket
    By ^NyAw^ in forum Qt Programming
    Replies: 3
    Last Post: 12th May 2008, 13:06
  4. Spawn a QThread in another QThread
    By david.corinex in forum Qt Programming
    Replies: 2
    Last Post: 19th December 2007, 12:54
  5. Is it possible to create a QThread without inheriting ?
    By probine in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 22:51

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.