Results 1 to 7 of 7

Thread: QtConcurent and QCache

  1. #1
    Join Date
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QtConcurent and QCache

    My aim is to make my calculation as fast as possible. I have an object that will calculate and store results in an array and hold those values until its life - meaning the object will just the do the long calculation for the first time then throughout its life it will just fetch from its array of values. fetching from its array is much faster than calculating it again. Each object is responsible for a record in my millions of records.

    I used QtConcurrent to do my "get all objects that i need " and ask each object for its values (which will give me an array). This will maximize my computational power using threads. This is all working but then all the object in each iteration on each thread is created and deleted. There are times that some iterations can operate on the same object the i could have save some calculation cost if i did not delete the object. This leads me to caching.

    The problem with caching is that the object that i cache is created from other thread so techinically i am breaking the laws of threading

    Have you have any scenarion like this and came up with an elegant solution?

    Qt Code:
    1. MyObject* DataSource::getObject(int index)
    2. {
    3. //check if its in cache
    4. MyObject* o = isInCache(index);
    5. if (o) return o;
    6. //if not
    7. o = new MyObject();
    8. addToCache(index,o); //note : o will be created under whoever thread is calling so the next time i will give to other thread this will error out
    9. return o;
    10. }
    11. struct MyInfo
    12. {
    13. int index;
    14. DataSource* source;
    15. }
    16. void process(MyInfo info)
    17. {
    18. MyObject* obj = info.source->getObject(info.id)
    19. QVector < double> values = obj->getMeValues();
    20. ....
    21. }
    22.  
    23. MyClass::fillValues()
    24. {
    25. //somewhere i got my datasource instantiated
    26. //get all index
    27. QVector<info> index = getAllIndexThatIneed();
    28. QFutureWatcher<void> fw = setFuture(QtConcurrent::map(index, process));
    29. //wait till finished
    30. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QtConcurent and QCache

    You can wrap your cache in a class that will provide appropriate synchronization using i.e. QReadWriteLock. It will slow down processing a bit but you won't wreck your cache.
    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
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QtConcurent and QCache

    I actually have a locker at my getObject but it did not help. My problem is that when i created MyObject it will be attached to the current thread, and some other thread was given by it when its available on my cache. I don't think there is racing issue, i think its just the limitation of having a cache.

    I have another problem to deal with when cache deletes the objects to make room and maybe the object is used by other thread. i think i will deal with this later .. or should i .. or maybe they are of the same nature..

    thanks for the inpit wysota.. keep it coming.

    baray98

    Qt Code:
    1. MyObject* DataSource::getObject(int index)
    2. {
    3. QMutexLocker (&mutex) ; //<--this exist but it did not help
    4. //check if its in cache
    5. MyObject* o = isInCache(index);
    6. if (o) return o;
    7. //if not
    8. o = new MyObject();
    9. addToCache(index,o); //note : o will be created under whoever thread is calling so the next time i will give to other thread this will error out
    10. return o;
    11. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QtConcurent and QCache

    Does MyObject inherit QObject?
    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
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QtConcurent and QCache

    yes .. is it bad?

    baray98

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

    Default Re: QtConcurent and QCache

    Yes, it is. QObjects are not thread-safe, you can't use them from within different threads at once.
    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
    Aug 2007
    Posts
    275
    Thanks
    28
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QtConcurent and QCache

    So if i pull this MyObject out of QObject should solve my threading problem .. interesting .. i should try it.

    baray98

Similar Threads

  1. qtconcurent imagescaling example upgrade
    By codebehind in forum Qt Programming
    Replies: 5
    Last Post: 11th September 2009, 13:40

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.