Page 2 of 2 FirstFirst 12
Results 21 to 24 of 24

Thread: Loading data in a thread

  1. #21
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Loading data in a thread

    Quote Originally Posted by wysota View Post
    As I said at the very beginning, I don't see the point of all this discussion, there is a simple rule of thumb that accessing non-thread-safe data from more than one thread requires proper synchronisation.
    And my reply to that is still the same. I want to understand why the problem occurs and not just stupidly follow a rule of thumb. And since the gui thread is only reading, I don't see how the data in the QList can be corrupted. If accessing an implicitely shared container by the [] operator can cause a copy of the data, then that would be a valid reason why it breaks. But it would be nice to have stronger confirmation than "it's not declared thread-safe". It's already not thread-safe if the reading is not guaranteed to work. But the reading is not the problem, the writing is. I will invest some time now and try to extract the relevant portion of the code, so that you can see it.

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

    Default Re: Loading data in a thread

    Quote Originally Posted by Cruz View Post
    And my reply to that is still the same. I want to understand why the problem occurs and not just stupidly follow a rule of thumb.
    Be my guest, dive in: http://qt.gitorious.org/qt/qt/trees/4.7/src

    I don't see how the data in the QList can be corrupted.
    The fact is it gets corrupted. So either you are wrong that the data can't be corrupted when one thread is reading it and the other is writing it or you are wrong that one thread is reading it and the other is writing it. I still have not seen your code, so I can only guess that somehow somewhere (maybe unwillingly) you are making a copy of the object which directs you to the path I was describing.

    If accessing an implicitely shared container by the [] operator can cause a copy of the data, then that would be a valid reason why it breaks. But it would be nice to have stronger confirmation than "it's not declared thread-safe".
    Ok, let's make my hint less subtle: Show us your code!
    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.


  3. #23
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Loading data in a thread

    So here is the code extract as it was before the mutex.

    Qt Code:
    1. // One frame of data.
    2. struct Frame
    3. {
    4. double x;
    5. double y;
    6. double z;
    7. }
    8.  
    9. class FileLoader
    10. {
    11. // The data structure. A QList of Frame structs.
    12. QList<Frame> data;
    13. }
    14.  
    15. // This is the loader thread.
    16. void FileLoader::run()
    17. {
    18. Frame f;
    19.  
    20. QFile file("data.dat");
    21. file.open(QIODevice::ReadOnly);
    22. QDataStream in(&file);
    23.  
    24. int frameCount = 0;
    25. in >> frameCount;
    26. for (int i = 0; i < frameCount; i++)
    27. {
    28. in >> f.x;
    29. in >> f.y;
    30. in >> f.z;
    31.  
    32. // Here is where a new frame is appended to the QList.
    33. data << f;
    34.  
    35. if (i % 1000 == 0)
    36. emit fileLoadingProgress(100*i/frameCount);
    37. }
    38. }
    39.  
    40.  
    41. // The GUI class.
    42. class GUI
    43. {
    44. FileLoader loader; // It has the FileLoader.
    45. Frame* currentFrame; // And a pointer to the frame currently displayed on the screen.
    46. double t; // This is a time parameter that is mapped to the data index.
    47. double tscale; // This influences how fast the time grows (so the speed of the animation).
    48. }
    49.  
    50. GUI::GUI(QWidget *parent)
    51. {
    52. loader.start(); // It starts the FileLoader thread in the constructor.
    53. t = 0.0;
    54. tscale = 1.0;
    55. }
    56.  
    57. // The animate function is periodically called by a timer.
    58. // It changes the currentFrame pointer by mapping the time t to a data index.
    59. void GUI::animate()
    60. {
    61. if (t + tscale < loader.data.size()-1)
    62. {
    63. t = t + tscale; // t is a double so that it can grow with arbitrary speed.
    64.  
    65. currentFrame = &(loader.data[(int)t]); // <-- currentFrame pointer assignment.
    66. }
    67.  
    68. draw();
    69. }
    To copy to clipboard, switch view to plain text mode 


    Btw, I did not say that the mutex solved the problem. I just said I put it in and it didn't slow anything down. I need to observe the software for a while to see if the broken frames still occur.

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

    Default Re: Loading data in a thread

    As far as I can see your animate() method uses the list as an lvalue (or whatever it was called) so it's likely the list is copied there. You can try using the at() method instead but it will return a copy of the item in the list which will make your currentFrame pointer invalid (as you return a pointer to a temporary object).
    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: 1
    Last Post: 19th July 2010, 11:43
  2. Replies: 6
    Last Post: 29th April 2009, 18:17
  3. stylesheet loading issue - thread
    By talk2amulya in forum Qt Programming
    Replies: 8
    Last Post: 26th February 2009, 12:53
  4. Loading a QPixmap from raw data
    By pherthyl in forum Qt Programming
    Replies: 1
    Last Post: 27th January 2008, 10:12
  5. Stop the thread during recursivly loading directory
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 14th May 2007, 19:02

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.