Results 1 to 20 of 24

Thread: Loading data in a thread

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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

    Ok I think I understand. Is it because the [] operator is overloaded and might cause a copy of the whole data, because one thread is reading it and another one is writing it? And then what happens is what Wysota said?

    If this is true, the problem would vanish if I used a plain old array of structs instead of the QList, right?

  2. #2
    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
    If this is true, the problem would vanish if I used a plain old array of structs instead of the QList, right?
    Not necessarily. Adding new items to the list may not be atomic so one of your threads (the reading one) may crash your program.
    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. #3
    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

    I was thinking of a fixed size array of structs, not a list in this case. And the structs don't contain any pointers. So the worst thing that can happen is that the reading thread gets bogus data when trying to access elemets, that have not been written yet.

  4. #4
    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

    First you should ask yourself a question - what do you want to achieve exactly. I'm not sure why you are writing to the structure and want to read it at the same time. I'm assuming that you are probably loading data from some file and want to show the user a "live" representation of the structure as it gets filled. I don't understand though why are you so reluctant to use a mutex, it seems a perfect solution here. If you add a bit of smart logic around it, the final effect should be really good.
    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.


  5. #5
    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
    I'm assuming that you are probably loading data from some file and want to show the user a "live" representation of the structure as it gets filled.
    Precisely.


    Quote Originally Posted by wysota View Post
    I don't understand though why are you so reluctant to use a mutex, it seems a perfect solution here. If you add a bit of smart logic around it, the final effect should be really good.
    I was a little bit reluctant, because people say it slows things down. But mostly I wanted an explanation why it makes sense, because I didn't know that reading from the QList can trigger copying. The mutex is in place now and there is no noticable change in the performance. But now it bugs me that in the background the data structure gets copied thousands of times even though it's totally unnecessary.

  6. #6
    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
    I was a little bit reluctant, because people say it slows things down.
    Only when threads try to enter the critical section at the same time but even if they do, you wouldn't even notice the slowdown. You get more slowdown by refreshing the gui thread too often.

    But mostly I wanted an explanation why it makes sense, because I didn't know that reading from the QList can trigger copying.
    I didn't say that.

    The mutex is in place now and there is no noticable change in the performance. But now it bugs me that in the background the data structure gets copied thousands of times even though it's totally unnecessary.
    So don't copy it.
    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.


  7. #7
    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
    I didn't say that.
    So then why does my data get corrupted in the first place?

  8. #8
    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

    I haven't seen your code, how could I answer your question?
    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.


  9. #9
    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

    Ok, so the whole thread was pointless so far?

    Can you please answer just this one specific question, that I already asked several times. If one thread is only reading from a QList and another thread is only writing, can it happen that at least one element of the QList gets corrupted for good? And again I emphasize that reading a half written element is NOT the point. There are errors in the data that remain even after the writing thread has finished its job.

  10. #10
    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
    Ok, so the whole thread was pointless so far?
    I can't tell you what is wrong with your code because I didn't see it. I can only tell you what might be wrong with your code based on your description and that's what I did. Since adding a mutex solved the problem it is more likely that I was correct but I can't be sure because I haven't seen your code.

    If one thread is only reading from a QList and another thread is only writing, can it happen that at least one element of the QList gets corrupted for good?
    Yes, I believe so since QList is not declared thread-safe for a reason.

    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.
    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.


  11. #11
    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.

  12. #12
    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.


  13. #13
    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.

  14. #14
    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.