Results 1 to 4 of 4

Thread: synchronize threads

  1. #1
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default synchronize threads

    I create two threads,and they share class A;
    Qt Code:
    1. class A
    2. {
    3. public:
    4. void method_1()
    5. {
    6. QMutexLocker locker(&mutex);
    7. ......
    8. method_2();
    9. ......
    10. }
    11. void method_2()
    12. {
    13. QMutexLocker locker(&mutex);//Does this code need?
    14. ........
    15. }
    16. public:
    17. mutable QMutex mutex;
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 
    I do not know the second locker is need or not,please please tell me.

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: synchronize threads

    That depends on whether the code in method_2 is allowed to be executed when method_1 is executed.

    Basically:
    * if the things should not be executed at the same time, you must guard them with a QMutex:
    * if it is ok that one thread is in method_1 and the other in method_2, you can guard them with 2 separate QMutexes (which will reduce blocking).

    So:
    + if no 2 threads should execute method_1 at the same time: define a QMutex and put a lock inside method_1
    + if no 2 threads should execute method_2 at the same time (without regarding method_1): define a (different) QMutex and put a lock inside method_2
    + if no 2 threads should execute any methods of your class in parallel (because all methods might access the same data): define a common QMutex and lock it in all methods.

    HTH

  3. #3
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: synchronize threads

    whether a method(in the shared class) needs to synchronize or not depending on the data visited by the method.For example (class A is shared by multi_threads):
    1.method_1 like this does not need to synchronize.
    Qt Code:
    1. void method_1()
    2. {
    3. int varable1;
    4. varable1=a.varable1//only read varable1,not write varable1
    5. }
    To copy to clipboard, switch view to plain text mode 
    2.method_1 like this needs to synchronize.
    Qt Code:
    1. void method_1()
    2. {
    3. int varable1;
    4. caculate(varable1);
    5. a.varable1=varable1;//write varable1
    6. }
    To copy to clipboard, switch view to plain text mode 
    Am I right or not?

  4. #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: synchronize threads

    Please read some books or articles about multithreading. Your questions have nothing to do with Qt and you can't ask us each time you are wondering if you should protect some piece of data with a mutex. I can give you a hint that a general rule of a thumb is that if a variable (or some other piece of data) can be accessed simoultaneously by more than one thread, you need to make sure there is no situation where one thread tries to read the data when the other changes it or when more than one thread tries to modify the variable. There are classes in Qt that help you do that - QMutex, QReadWriteLock, QWaitCondition, QSemaphore. You have the docs -- please read it.
    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. Replies: 8
    Last Post: 27th March 2013, 11:51
  2. Why do some QWidgets create own threads?
    By donglebob in forum Qt Programming
    Replies: 5
    Last Post: 6th July 2010, 17:01
  3. No context switch between Threads and parent ?
    By donglebob in forum Qt Programming
    Replies: 15
    Last Post: 29th October 2008, 22:49
  4. Once more: Threads in Qt4
    By high_flyer in forum Qt Programming
    Replies: 5
    Last Post: 9th August 2006, 18:35

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.