Results 1 to 3 of 3

Thread: Sharing a variable of type std::vector between QThread classes

  1. #1
    Join Date
    Jun 2013
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Sharing a variable of type std::vector between QThread classes

    Hello! I have a C++ library based on Qt 5 that uses multiple threads (classes inherited from QThread) for some computations. I need to share a STL vector between them (threads don't perform any pushing/popping with this vector, but they change it's values). I use the special wrapper around std::atomic:

    Qt Code:
    1. struct AtomicUInt64
    2. {
    3. std::atomic<uint64_t> atomic;
    4.  
    5. AtomicUInt64() : atomic ( uint64_t() ) {}
    6.  
    7. explicit AtomicUInt64 ( uint64_t const &v ) : atomic ( v ) {}
    8. explicit AtomicUInt64 ( std::atomic<uint64_t> const &a ) : atomic ( a.load() ) {}
    9.  
    10. AtomicUInt64 ( AtomicUInt64 const &other ) : atomic ( other.atomic.load() ) {}
    11.  
    12. AtomicUInt64 &operator= ( AtomicUInt64 const &other )
    13. {
    14. atomic.store ( other.atomic.load() );
    15. return *this;
    16. }
    17.  
    18. AtomicUInt64 operator++ ()
    19. {
    20. this->atomic++;
    21. return *this;
    22. }
    23. };
    To copy to clipboard, switch view to plain text mode 

    And std::vector in a data structure:

    Qt Code:
    1. struct ThreadInput
    2. {
    3. std::vector<AtomicUInt64> *atomsUsed = NULL;
    4. //other variables
    5. };
    To copy to clipboard, switch view to plain text mode 

    I initialize this structure in a class that manages threads:

    Qt Code:
    1. CalculationThread threadData;
    2. threadData.myVect = new std::vector<AtomicUInt64>();
    3. for ( int x = 0; x < 100; ++x )
    4. {
    5. AtomicUInt64 value ( 0 );
    6. threadData.myVect->push_back ( value );
    7. }
    8. //assign other variables in 'threadData'
    9. for ( int x = 0; x < 2; ++x )
    10. {
    11. MyThread *thread = new MyThread ( threadData );
    12. thread->start();
    13. }
    To copy to clipboard, switch view to plain text mode 

    Finally, this is MyThread's 'void run()' impementation:

    Qt Code:
    1. void MyThread::run()
    2. {
    3. uint64_t ull;
    4. for ( ull = 0; ull < this->threadData.iterations; ++ull)
    5. {
    6. //increment the 100th value of 'myVect'
    7. ++this->threadInput.myVect->data()[99];
    8. }
    9. qDebug() << "Finished = " + QString::number(this->thredData.myVect->data()[99].atomic) + " | it = " + QString::number(ull);
    10. }
    To copy to clipboard, switch view to plain text mode 

    So, when I set 'threadData.iterations' variable to ~490000 (this value is different for different threads), I get the following in Application Output:

    "Finished = 491760 | it = 249654"
    "Finished = 499308 | it = 499308"

    And this is not what I expected to see. Why the first finished thread outputs that the 100th value of vector is 249654? Shouldn't it be more than iteration count, as there were 2 threads?
    Maybe there are another ways to share a STL vector between instances of `MyThread` class inherited from QThread and assume that it is atomic?

  2. #2
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Sharing a variable of type std::vector between QThread classes

    Maybe there are another ways to share a STL vector between instances of `MyThread` class inherited from QThread and assume that it is atomic?
    No there is no special way to share a std::vector or any c++ class for that matter.
    And this is not what I expected to see. Why the first finished thread outputs that the 100th value of vector is 249654? Shouldn't it be more than iteration count, as there were 2 threads?
    if your data, myVect is shared between the 2 threads you have created, I don't know why the result is alarming? Or is myVect/thread. How is it sharing then. Your datastructures are not clear, at least, to me.
    Moreover, it is generally not required to sub-class QThread. QThread derived class stays in the thread you have created. Objects created in the run() have their affinity to the new thread. Since this is a tricky point missed by most, this is what the documentation has to say about subclassing QThread...http://doc.qt.io/qt-4.8/qthread.html#subclassing-qthread

  3. #3
    Join Date
    Jun 2013
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Sharing a variable of type std::vector between QThread classes

    Quote Originally Posted by pkj View Post
    Or is myVect/thread. How is it sharing then. Your datastructures are not clear, at least, to me.
    Sorry if I was not clear. I send the same pointer to std::vector object to threads. So they should use the same object in memory - and this object is std::vector. Threads increment the same value in this vector by one in the loop. When the loop ends on the last running thread, I expect to see in Application Output the value equals to (loop iterations * threads count), but I see another numbers.

Similar Threads

  1. To Share a variable among classes
    By JenniferF in forum Newbie
    Replies: 2
    Last Post: 12th May 2011, 11:13
  2. Replies: 1
    Last Post: 9th April 2011, 21:07
  3. Classes with Implicit Sharing
    By weaver4 in forum Newbie
    Replies: 5
    Last Post: 15th January 2011, 18:57
  4. How to correctly subclass classes that use implicit sharing
    By Gh0str1d3r in forum Qt Programming
    Replies: 1
    Last Post: 16th August 2010, 09:42
  5. Replies: 4
    Last Post: 29th December 2008, 19:50

Tags for this Thread

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.