Results 1 to 16 of 16

Thread: QMutex and QDataStream

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default QMutex and QDataStream

    hi
    i need to protect the access of a variable which is currently being filled with the QDataStream(in a multithreading program).

    in the following code does the access of the int variable blocksize is locked or the datastream is locked ,or both.

    QDataStream datastream(socket);
    QMutex mutex;
    mutex.lock();
    datastream >> blocksize;
    mutex.unlock();

  2. #2
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanked 86 Times in 81 Posts

    Default Re: QMutex and QDataStream

    None. You don't lock variables.
    The only thing you do here is that you make sure that the code line between lock() and unlock() is not executed by two or more threads at the same time. Nothing more.

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

    Default Re: QMutex and QDataStream

    Actually even that is not the case here as each thread will have its own mutex instance so they won't be exclusive. The above code is really a no-op (apart from the line operating on the stream).

  4. #4
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QMutex and QDataStream

    thanks for u r replys

    here is my header file.

    //plot.h Include files are not specified to reduce the post size
    Qt Code:
    1. class th : public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. th();
    7. void run();
    8.  
    9. quint32 blocksize;
    10.  
    11. private slots:
    12. void read_socket();
    13.  
    14. private:
    15. QTcpSocket *socket;
    16. };
    17.  
    18. class PLOT : public QMainWindow
    19. {
    20. Q_OBJECT
    21. public:
    22. PLOT();
    23.  
    24. private:
    25. th t;
    26. };
    27.  
    28. #endif /*PLOT_H_*/
    To copy to clipboard, switch view to plain text mode 

    //plot.cpp
    Qt Code:
    1. th::th() :blocksize(0)
    2. {
    3.  
    4. }
    5.  
    6. void th::run()
    7. {
    8. socket=new QTcpSocket();
    9. socket->connectToHost("192.168.1.6", 9999);
    10. connect(socket, SIGNAL(readyRead()), this, SLOT(read_socket()));
    11. exec();
    12. }
    13.  
    14. void th::read_socket()
    15. {
    16. QDataStream in(socket);
    17. if (blocksize == 0)
    18. {
    19. if (socket->bytesAvailable() < (int)sizeof(quint32))
    20. return;
    21.  
    22. in >> blocksize;
    23. }
    24. }
    25.  
    26. PLOT::PLOT()
    27. {
    28. t.start();
    29.  
    30. //waiting here till the t.blocksize variable is getting filled but had not worked and never
    31. //gets out of the loop,i have tried declaring the blocksize variable in the [B]th[/B]
    32. //class as[B] volatile quint32 blocksize[/B] and even it has not worked.
    33. //only [B]works[/B] when introduce a
    34. //delay here as [B]QTest::qWait(3000);[/B]
    35.  
    36. while (t.blocksize == (qint32)0)
    37. {
    38. qDebug()<<" PLOT inside while "<<t.blocksize;
    39. coloum=(int)(t.blocksize/360);
    40. }
    41. }
    To copy to clipboard, switch view to plain text mode 

    where should i modify the code to work.

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

    Default Re: QMutex and QDataStream

    Read about QWaitCondition please.

  6. #6
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QMutex and QDataStream

    If i have to use the QWaitCondition i have to use the mutex,which means i need two classes subclassed from QThread , and also i have to do gui in my program.i tried to use QWaitCondition but i am stuck,i am unable to figure where to wait and where to wakeup the thread(one of the class in the above program does not inherit QThread.)

    Do i have to have multiple inheritance(QThread and QWidget ) to solve the above problem in which i have do both gui and threading.


    why the above code never gets out of while.
    here is a sample code
    Qt Code:
    1. #include <QtGui>
    2. #include <QtNetwork>
    3.  
    4. class trd : public QThread
    5. {
    6. public:
    7. void run()
    8. {
    9. while (1)
    10. qDebug()<<"run";
    11. }
    12. };
    13.  
    14. class sub : public QWidget
    15. {
    16. public:
    17. sub()
    18. {
    19. trd th;
    20. th.start();
    21.  
    22. while (1)
    23. qDebug()<<"sub";
    24. }
    25. };
    26.  
    27. int main(int argc, char *argv[])
    28. {
    29. QApplication a(argc, argv);
    30. sub t;
    31. return a.exec();
    32. }
    To copy to clipboard, switch view to plain text mode 

    in this code both the run and sub gets printed alternatively , the same idea i am using to make work in the program(waiting for the while condition to fail) in the above post.where did i made the error.

    thanks
    Last edited by babu198649; 11th April 2008 at 14:14.

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

    Default Re: QMutex and QDataStream

    I don't really understand what you mean by "where to wait and where to wake"... Don't you know where you want to halt execution of one thread and when to let it run? It is your algoritm - you should know best.

  8. #8
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QMutex and QDataStream

    Consider two classes class GUI(which subclasses QWidget) and class THD(which subclasses QThread) ,now if i want to wait in class G for a condition which is set by class THD ,what should i do.

    here is a simple code what i have explained above.
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class THD : public QThread
    4. {
    5. public:
    6. int i;
    7.  
    8. THD():i(0)
    9. {
    10.  
    11. }
    12.  
    13. void run()
    14. {
    15. i=1;
    16. }
    17. };
    18.  
    19. class GUI : public QWidget
    20. {
    21. public:
    22. GUI()
    23. {
    24. THD th;
    25. th.start();
    26.  
    27. //wait until i is changed and then continue(how to wait)
    28. }
    29. };
    30.  
    31. int main(int argc, char *argv[])
    32. {
    33. QApplication a(argc, argv);
    34. GUI t;
    35. return a.exec();
    36. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QMutex and QDataStream

    Call wait() where you want to wait and wakeAll() where you want to release the other thread. What is the problem?

    Qt Code:
    1. GUI(){THD th; th.start(); wc.wait(&m); }
    2. ...
    3. void run(){ i=0; wc.wakeAll(); }
    To copy to clipboard, switch view to plain text mode 

    By the way, in this case you could easily use signals and slots instead of a mutex...

  10. The following user says thank you to wysota for this useful post:

    babu198649 (11th April 2008)

  11. #10
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: QMutex and QDataStream

    thanks wysota
    But the widget is not getting displayed after adding the wc.wait(&m) (i have called show() in main). it seems that it gets stopped until the wc.wakeAll() is called .But i want it to wake according to the buttons pressed in the GUI widget.
    should i have to start the thread in a slot.

    Qt Code:
    1. in this case you could easily use signals and slots instead of a mutex
    To copy to clipboard, switch view to plain text mode 
    the above code is just to explain my problem . i am using the ideas in different program which is large to post here.
    Last edited by babu198649; 11th April 2008 at 15:42.

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

    Default Re: QMutex and QDataStream

    Quote Originally Posted by babu198649 View Post
    it seems that it gets stopped until the wc.wakeAll() is called
    Isn't that what you wanted? You wanted to stop execution of a thread until a variable is changed in another thread. That's exactly what happens.

    in this case you could easily use signals and slots instead of a mutex
    the above code is just to explain my problem . i am using the ideas in different program which is large to post here.
    Then maybe you should try expressing your problems more clearly. If you describe your problem incorrectly, you can only blame yourself for getting a wrong answer. Without seeing your code I can only say "redesign code to use signals and slots and rely on the event queue for synchronization".

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
  •  
Qt is a trademark of The Qt Company.