Results 1 to 20 of 21

Thread: qt multi threads

Hybrid View

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

    Default Re: qt multi threads

    A class(defined by myself) shared by the threads must be lock and unlock?

  2. #2
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qt multi threads

    yes, using mutex

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

    Default Re: qt multi threads

    when I used two threads like what I have said in my program and compile and run it,it will be crashed at radom position,Is it because of my not synchronizing my threads?Must I synchronize them?

  4. #4
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qt multi threads

    if there is a shared object, u have to make it thread-safe..and yes, synchronize..and again, why cant u upgrade Qt? u have a solution waiting for u in there, so i cant understand ur reluctance

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

    Default Re: qt multi threads

    Thanks for answer.
    1.Why have not upgrate my Qt?
    My colleagues and I all use qt4.3.1+ mysql+coin3d+msvc2005+carnac.If I upgrate my qt,they must.And a lot time will be wasted for built platform again.
    2.If threads only read(not write) data from the shared class(created by myself),Must it be locked and unlocked?like this:
    .....
    Qt Code:
    1. class Mythread1
    2. {
    3. public:
    4. Mythread1(SharedClass *sharedclass);
    5. void calculate(SharedClass *sharedclass)
    6. {
    7. //read data from sharedclass and calculate
    8. }
    9. }
    10.  
    11.  
    12. SharedClass *sharedClass=new SharedClass ();
    13. init(A);
    14. thread1=new Mythread1(A);
    15. thread2=new Mythread1(A)
    16. .....
    To copy to clipboard, switch view to plain text mode 
    Do I use mutex in SharedClass ?That is,must all the functions and varables in SharedClass be locked and unlocked?for example ,SharedClass like this:
    Qt Code:
    1. class SharedClass
    2. {
    3. public:
    4. A *a;
    5. B *b;
    6. QString str;
    7. void function1();
    8. }
    To copy to clipboard, switch view to plain text mode 
    How could I do?
    Thank you.
    Last edited by wysota; 7th April 2009 at 15:09. Reason: missing [code] tags

  6. #6
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qt multi threads

    read about thread-safe classes.. thread-safe classes are those classes for which multiple threads can access the SAME instance of the class..u have to provide synchronization in these classes..i dunno what your application is doing..u have a number of possibilities to implement synchronisation depending upon what ur class does..

    and you should consult with your fellow colleagues about the new Qt version...there r lots of other features that u will be missing and implementing yourself..which is MORE wasted time

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

    Default Re: qt multi threads

    class SharedClass will be shared by all threads,and it structure is like this:
    Qt Code:
    1. class SharedClass
    2. {
    3. public:
    4. A *a;
    5. B *b;
    6. QString str;
    7. void function1()
    8. {
    9. a=new A();
    10. b=new B();
    11.  
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    If I will let it thread-safe,Do I do like this?
    Qt Code:
    1. class SharedClass
    2. {
    3. public:
    4. A *a;
    5. B *b;
    6. QString str;
    7. void function1()
    8. {
    9. QMutexLocker locker(&mutex);
    10. a=new A();
    11. b=new B();
    12.  
    13. }
    14. private:
    15. mutable QMutex mutex;
    16. }
    To copy to clipboard, switch view to plain text mode 
    class a and b must be thread-safe also?
    Last edited by wysota; 7th April 2009 at 15:10. Reason: missing [code] tags

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

    Default Re: qt multi threads

    no answer ??

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

    Default Re: qt multi threads

    Quote Originally Posted by weixj2003ld View Post
    no answer ??
    We have no idea what you are asking for. At least I know I don't...
    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.


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

    Default Re: qt multi threads

    Quote Originally Posted by wysota View Post
    We have no idea what you are asking for. At least I know I don't...
    Sorry.
    I explain my question again.
    As we know ,if a class is shared by muti-threads,it should be thread-safe.Now I define a class ,and it will be shared by 2 threads.If I want to let it thread-safe,I do as follow,Am I right or not?

    Qt Code:
    1. class SharedClass
    2. {
    3. public:
    4. A *a;
    5. B *b;
    6. QString str;
    7. void function1()
    8. {
    9. a=new A();
    10. b=new B();
    11.  
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    If I will let it thread-safe,I add code like this:

    Qt Code:
    1. class SharedClass
    2. {
    3. public:
    4. A *a;
    5. B *b;
    6. QString str;
    7. void function1()
    8. {
    9. QMutexLocker locker(&mutex);
    10. a=new A();
    11. b=new B();
    12.  
    13. }
    14. private:
    15. mutable QMutex mutex;
    16. }
    To copy to clipboard, switch view to plain text mode 
    By the way,Must class a and b be thread-safe?
    Last edited by wysota; 8th April 2009 at 13:06. Reason: missing [code] tags

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

    Default Re: qt multi threads

    If you post a code snippet without appropriate tags one more time, you will get a temporary ban on the forum.

    No, a and b don't have to be thread safe.
    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. i'm in troubles withe threads !!
    By jiboon in forum Newbie
    Replies: 3
    Last Post: 21st April 2008, 20:36
  4. QThread - multi threaded signals and slots
    By rishid in forum Qt Programming
    Replies: 4
    Last Post: 30th March 2008, 01:47

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.