Results 1 to 3 of 3

Thread: how to create two thread and acess the same data member of the class

  1. #1
    Join Date
    Mar 2011
    Location
    delhi
    Posts
    45
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation how to create two thread and acess the same data member of the class

    hi
    all i want two create two thread,by these two thread i want to access member function of the class which are manipulating the data member of the class,how can i access these function by the two different threads in Qt.

    please help

    thanks with regards:
    gauravg

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: how to create two thread and acess the same data member of the class

    Why do you think you need two threads?
    What is the task you have to achieve?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Oct 2010
    Posts
    55
    Thanks
    1
    Thanked 11 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    9

    Default Re: how to create two thread and acess the same data member of the class

    Same answer as above, only a bit more elaborate since I had already started writing

    The QThread class could be what you are looking for together with QMutex to ensure serialized access to shared data. Most likely though, you don't want to use threads at all. Contrary to popular belief, threads do not improve performance of your program. They can however make it more responsive by creating the illusion of performing multiple tasks at once. In Qt you have several ways of achieving this without directly using low-level threading primitives. So the question is really; are threads the best solution in this particular case?

    Before subclassing QThread and reimplementing QThread::run(), you should consider some other options:

    • The QtConcurrent API, where the actual threading code is "hidden".
    • To use a QObject with different thread affinity as a "worker".

    Qt Code:
    1. QThread *thread = new QThread(this);
    2. Worker* worker = new Worker; // Worker is a QObject subclass
    3. worker->moveToThread(thread);
    4. thread->start();
    5. QMetaObject::invokeMethod(worker, "doSomething");
    6.  
    7. // and eventually worker needs to be destroyed somewhere
    8. delete worker;
    To copy to clipboard, switch view to plain text mode 
    ...or any of the other suggestions, as outlined here:

    http://doc.qt.nokia.com/4.7-snapshot/thread-basics.html
    http://doc.qt.nokia.com/qq/qq27-responsive-guis.html

Similar Threads

  1. accessing static member variables of one class in another class
    By jasonknight in forum General Programming
    Replies: 5
    Last Post: 6th September 2010, 15:53
  2. Replies: 4
    Last Post: 29th May 2010, 13:56
  3. A class member has ran away?!
    By MIH1406 in forum Newbie
    Replies: 4
    Last Post: 25th August 2009, 00:21
  4. Replies: 3
    Last Post: 16th May 2007, 12:07
  5. Replies: 5
    Last Post: 14th July 2006, 23:42

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.