Results 1 to 7 of 7

Thread: QMutex

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

    Default QMutex

    When I create two threads,and synchronize them with QMutex,Do I define a mutex(QMutex) as a local varable(that is,mutex is defined respectively in two threads) or a global one(that is ,the two threads visit the same mutex)?

  2. #2
    Join Date
    Nov 2008
    Location
    Częstochowa/Poland
    Posts
    50
    Thanks
    2
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMutex

    The two threads need to work on the same QMutex object.

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

    Default Re: QMutex

    but in the mandelbrot (an example of qt4.3.1) ,the mutex is defined in the child thread,why?

  4. #4
    Join Date
    Nov 2008
    Location
    Częstochowa/Poland
    Posts
    50
    Thanks
    2
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMutex

    That got me thinking. But if You look at the MandelbrotWidget class only one worker thread is used. AFAIK to protect your data with a mutex all thread accessing the data need to operate on the same mutex object, be it via global definition, pointer or reference.

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

    Default Re: QMutex

    Thank u for ur answer.
    your means is that:If the sharing data(visited by all threads) is global defination , a pointer or reference,I must define a mutex and it must be a global one?

  6. #6
    Join Date
    Nov 2008
    Location
    Częstochowa/Poland
    Posts
    50
    Thanks
    2
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMutex

    If the sharing data(visited by all threads)
    If the data can be accessed by all the threads (regardless of the way the data is stored) You need to protect it with a mutex.
    be it via global definition, pointer or reference
    This part was just a hint that You can define your thread class to store the mutex via a pointer.
    Qt Code:
    1. Class MyThread : public QThread
    2. {
    3. public:
    4. MyThread(QMutex* mutex);
    5. /* run etc.. */
    6. private:
    7. QMutex* myMutex;
    8. };
    To copy to clipboard, switch view to plain text mode 

  7. #7
    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: QMutex

    @weixj2003ld: I really suggest you get some book on multithreading. Silberschatz's book on operating systems is a classic so you might want to have a look at it. You're asking basic questions on mutexes over and over again and it seems you are not learning from the answers you are getting because you come back and ask almost the same question again. If you are working on a school assignment, which it seems you are, we can help you in a limited scope only - we have a rule of not solving school assignments for people. So unless you prove me wrong, I'll be closely monitoring your threads and deleting all replies that look like complete solutions to your multithreading questions. We want to help you but we will not solve your school tasks for you or else you will learn nothing from them. Bear in mind you are repeatedly asking questions that are practically out of the scope of this forum. Now please open your favourite search engine and type in "mutex" or "thread synchronization".
    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. New to QMutex
    By durbrak in forum Qt Programming
    Replies: 3
    Last Post: 12th March 2009, 21:16
  2. threads synchronization and Qmutex
    By jiboon in forum Newbie
    Replies: 2
    Last Post: 3rd May 2008, 17:10
  3. QMutex and QDataStream
    By babu198649 in forum Newbie
    Replies: 15
    Last Post: 12th April 2008, 12:25
  4. QMutex is not working in release mode
    By bitChanger in forum Qt Programming
    Replies: 3
    Last Post: 25th April 2007, 13:32
  5. Replies: 3
    Last Post: 12th October 2006, 21:48

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.