Page 1 of 2 12 LastLast
Results 1 to 20 of 31

Thread: multi threading or QConcurrent for the scenario

  1. #1
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default multi threading or QConcurrent for the scenario

    Hi,

    I have a scenario where in I have to show a document on click of the document icon. Each page of the document is a separate html which is encrypted.
    Due to the huge number of pages, it take more time to decrypt all the html files and show the page.

    Kindly suggest which one is the best suitable (QThread / QConcurrent) , so that I can show the page once the 1st file is decrypted without waiting for all files to complete decrypting process. I want to make the page viewing and the decrypting happen simultaneously once the decrypting of the 1st file is done.

    Thanks in advance

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: multi threading or QConcurrent for the scenario

    As you don't need any background activity to be done continuously, it would be better to use QConcurrent, just run the decrypting function QtConcurrent::run().
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: multi threading or QConcurrent for the scenario

    Thanks for the quick response.

  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: multi threading or QConcurrent for the scenario

    I would rather put all pages into a container and use QtConcurrent::map().
    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
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: multi threading or QConcurrent for the scenario

    How do I use QtConcurrent::map()

    I have tried to implement QtConcurrent, but it crashes
    stating the below error
    "Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
    0 libsystem_kernel.dylib 0x00007fff8cb0f866 __pthread_kill + 10
    1 libsystem_pthread.dylib 0x00007fff87aee35c pthread_kill + 92
    2 libsystem_c.dylib 0x00007fff89f2fb1a abort + 125
    3 org.qt-project.QtCore 0x000000010c1a2159 qt_message_fatal(QtMsgType, QMessageLogContext const&, QString const&) + 9
    4 org.qt-project.QtCore 0x000000010c1a3611 QMessageLogger::fatal(char const*, ...) const + 161
    5 org.qt-project.QtCore 0x000000010c19e815 qt_assert_x(char const*, char const*, char const*, int) + 85
    6 com.yourcompany.learnOn 0x000000010b2cd880 QList<QFileInfo>:perator[](int) + 96 (qlist.h:486)
    7 com.yourcompany.learnOn 0x000000010b2cbf8a loadbook::decryptLoop(int, int) + 138 (loadbook.cpp:122)

    ASSERT failure in QList<T>:perator[]: "index out of range", file /Applications/QT/5.4/clang_64/lib/QtCore.framework/Headers/qlist.h, line 486
    The program has unexpectedly finished.


    The code is as below
    Qt Code:
    1. totalXHtmlPages = 173
    2. int index=0;
    3.  
    4. while( index <totalXHtmlPages)
    5. {
    6. if(stopThread) return;
    7.  
    8.  
    9. QFuture<QStringList> test = QtConcurrent::run(this,&loadbook::decryptLoop,index,index+13);
    10. PgContents.append(test.result());
    11.  
    12. index = index + 13;
    13. }
    14.  
    15.  
    16. QStringList loadbook::decryptLoop(int startIndex, int endIndex)
    17. {
    18. QStringList decryptContent;
    19. for(int index = startIndex; index < endIndex; index++)
    20. {
    21.  
    22. if(stopThread) break;
    23. decryptContent.append(decrypt->decryptFile(FileInfoList[index].absoluteFilePath(), 'A'));
    24. qDebug()<<"FileInfoList["<<index<<"] -> "<<FileInfoList[index].absoluteFilePath();
    25. }
    26. return decryptContent;
    27. }
    To copy to clipboard, switch view to plain text mode 
    Here I dont find multiple thread which I intended to create.

    how can I make this 173 files to be decrypted as fast as possible using threads
    Last edited by ejoshva; 6th March 2015 at 12:20.

  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: multi threading or QConcurrent for the scenario

    Your code makes no sense. You start a thread only to wait until it finishes executing. Use QtConcurrent::map() as advised.
    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
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: multi threading or QConcurrent for the scenario

    Qt Code:
    1. QList<QString> fileList;
    2. for(int j=1;j<=settings->childKeys().count();j++)
    3. {
    4. QString temp = bkpath+settings->value(QString::number(j)).toString();
    5. fileList.append(temp);
    6. }
    7. QFuture<QStringList> test = QtConcurrent::map(fileList,&Decrypt::decryptFile));
    8.  
    9.  
    10. QString Decrypt::decryptFile(QString sourceFile,QChar keystring)
    11. {
    12. _decrptedResStr.clear();
    13. string srcFile=sourceFile.toUtf8().constData();
    14. ifstream ffin;
    15.  
    16. ffin.open(srcFile.c_str(),ios_base::in|ios_base::binary);
    17.  
    18. char keyToEncryption= keystring.toLatin1();
    19. if(ffin.is_open())
    20. {
    21. char ch;
    22. while(!(ffin.read(&ch,1).eof()))
    23. {
    24. ch ^= keyToEncryption;
    25. _decrptedResStr.append(ch);
    26. }
    27. }
    28. ffin.close();
    29. return _decrptedResStr;
    30. }
    To copy to clipboard, switch view to plain text mode 
    But I get the error
    /Users/user/LearnOn/loadbook.cpp:171: error: no matching function for call to 'blockingMapped'
    QFuture<QStringList> test = QtConcurrent::map(fileList,&Decrypt::decryptFile)) ;
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~


    (My bad posted the wrong function , edited it )
    Last edited by wysota; 6th March 2015 at 11:54. Reason: missing [code] tags

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: multi threading or QConcurrent for the scenario

    That doesn't make much sense.
    Your list elements are of type QString, but your map function takes QChar.

    Cheers,
    _

  9. #9
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: multi threading or QConcurrent for the scenario

    Qt Code:
    1. QList<QString> test = QtConcurrent::map(fileList,&Decrypt::decryptLoop);
    2.  
    3. QString loadbook::decryptLoop(QString fileName)
    4. {
    5. return decrypt->decryptFile(fileName, 'A');;
    6. }
    To copy to clipboard, switch view to plain text mode 


    Even after modifying like this , I am still getting the same issue.
    /Users/user/LearnOn/loadbook.cpp:163: error: no matching function for call to 'map'
    QList<QString> test = QtConcurrent::map(fileList,&Decrypt::decryptFile);
    ^~~~~~~~~~~~~~~~~

    I am new to QT, kindly guide me to resolve this issue.
    Last edited by ejoshva; 6th March 2015 at 12:35.

  10. #10
    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: multi threading or QConcurrent for the scenario

    Your decryptFile() method is not re-entrant.
    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
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: multi threading or QConcurrent for the scenario

    how do I make it re-entrant?

  12. #12
    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: multi threading or QConcurrent for the scenario

    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 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: multi threading or QConcurrent for the scenario

    Quote Originally Posted by ejoshva View Post
    Even after modifying like this , I am still getting the same issue.
    /Users/user/LearnOn/loadbook.cpp:163: error: no matching function for call to 'map'
    QList<QString> test = QtConcurrent::map(fileList,&Decrypt::decryptFile);
    ^~~~~~~~~~~~~~~~~
    At least in your code snippet shown here you are still passing the wrong function.
    Also the return value of map() is QFuture<void>

    Cheers,
    _

  14. #14
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: multi threading or QConcurrent for the scenario

    Qt Code:
    1. QString loadbook::decryptLoop(QString &fileName)
    2. {
    3. QString content;
    4. content = decrypt->decryptFile(fileName, 'A');
    5. return content;
    6. }
    7.  
    8. QList<QString> test = QtConcurrent::map(fileList,&loadbook::decryptLoop);
    To copy to clipboard, switch view to plain text mode 

    Hope this is re-entrant.

    now the errors are
    /Users/user/LearnOn/loadbook.cpp:159: error: no viable conversion from 'QFuture<void>' to 'QList<QString>'
    QList<QString> test = QtConcurrent::map(fileList,&loadbook::decryptLoop) ;
    ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    /Applications/QT/5.4/clang_64/lib/QtConcurrent.framework/Headers/qtconcurrentmapkernel.h:63: error: no matching function for call to object of type 'QtConcurrent::MemberFunctionWrapper1<QString, loadbook, QString &>'
    map(*it);
    ^~~


    Added after 28 minutes:


    I tried with mappedReduced
    Qt Code:
    1. QFuture<QString> test = QtConcurrent::mappedReduced(fileList,&loadbook::decryptLoop,&loadbook::joinContent);
    2. test.waitForFinished();
    3.  
    4. QString loadbook::decryptLoop(QString &fileName)
    5. {
    6. QString content;
    7. content = decrypt->decryptFile(fileName, 'A');
    8. return content;
    9. }
    10. void joinContent(QString &reduceResult, const QString &partial) {
    11. reduceResult += partial;
    12. }
    To copy to clipboard, switch view to plain text mode 
    getting the below error
    /Users/user/LearnOn/loadbook.cpp:161: error: no matching function for call to 'mappedReduced'
    QFuture<QString> test = QtConcurrent::mappedReduced(fileList,&loadbook::de cryptLoop,&loadbook::joinContent);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~
    Last edited by ejoshva; 6th March 2015 at 13:10.

  15. #15
    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: multi threading or QConcurrent for the scenario

    Quote Originally Posted by ejoshva View Post
    Hope this is re-entrant.
    Depends what decryptFile() does. If you didn't change its code then it's not reentrant.
    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.


  16. #16
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: error: no matching function for call to 'mappedReduced'

    Qt Code:
    1. QString loadbook::decryptLoop(QString &fileName)
    2. {
    3. QString content;
    4. content = decrypt->decryptFile(fileName, 'A');
    5. return content;
    6. }
    7. void joinContent(QString &reduceResult, const QString &partial) {
    8. reduceResult += partial;
    9. }
    10.  
    11. QFuture<QStringList> test = QtConcurrent::mappedReduced(fileList,&loadbook::decryptLoop,&loadbook::joinContent,QtConcurrent::SequentialReduce);
    12. test.waitForFinished();
    To copy to clipboard, switch view to plain text mode 

    I have added a code like above so that I want all the files loaded in the fileList have to be decrypted simultaneously and the decrypted content are to be caught in the test.

    When I try to compile this code getting the error as below

    /Users/user/LearnOn/loadbook.cpp:142: error: no matching function for call to 'mappedReduced'
    QFuture<QStringList> test = QtConcurrent::mappedReduced(fileList,&loadbook::de cryptLoop,&loadbook::joinContent,QtConcurrent::Seq uentialReduce);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~
    /Applications/QT/5.4/clang_64/lib/QtConcurrent.framework/Versions/5/Headers/qtconcurrentmap.h:115: candidate template ignored: couldn't infer template argument 'ResultType'
    QFuture<ResultType> mappedReduced(const Sequence &sequence,
    ^
    /Applications/QT/5.4/clang_64/lib/QtConcurrent.framework/Versions/5/Headers/qtconcurrentmap.h:142: candidate template ignored: deduced conflicting types for parameter 'Iterator' ('QList<QString>' vs. 'QString (loadbook::*)(QString &)')
    QFuture<ResultType> mappedReduced(Iterator begin,
    ^
    /Applications/QT/5.4/clang_64/lib/QtConcurrent.framework/Versions/5/Headers/qtconcurrentmap.h:156: candidate template ignored: deduced conflicting types for parameter 'Iterator' ('QList<QString>' vs. 'QString (loadbook::*)(QString &)')
    QFuture<typename QtPrivate::ReduceResultType<ReduceFunctor>::Result Type> mappedReduced(Iterator begin,
    ^
    /Applications/QT/5.4/clang_64/lib/QtConcurrent.framework/Versions/5/Headers/qtconcurrentmap.h:128: candidate template ignored: substitution failure [with Sequence = QList<QString>, MapFunctor = QString (loadbook::*)(QString &), ReduceFunctor = void (loadbook::*)(QString &, const QString &)]: implicit instantiation of undefined template 'QtPrivate::ReduceResultType<void (loadbook::*)(QString &, const QString &)>'
    QFuture<typename QtPrivate::ReduceResultType<ReduceFunctor>::Result Type> mappedReduced(const Sequence &sequence,
    ~~~~~~~~~~~~~~~~ ^

    Kindly help to resolve this issue. I have been sitting with this for more than a day with this issue.


    Added after 1 21 minutes:


    Got the issue here, the functions decryptLoop() and joinContent() are to be static.
    But again one more issue is that, I need to call a non-static function from inside decryptLoop(), how do I do it
    Last edited by ejoshva; 7th March 2015 at 06:29.

  17. #17
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: multi threading or QConcurrent for the scenario

    I got the issue here. Issue is that the functions decryptLoop() and joinContents() are to be static.
    But decrypteFile() is non-static function which I am calling from decreyptLoop(). How do I do it?
    getting an error at this point like this.

  18. #18
    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: multi threading or QConcurrent for the scenario

    Until you modify your decryption function to be re-entrant, it doesn't really matter how you call that function, it will return invalid results.
    Last edited by wysota; 11th March 2015 at 06:48.
    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.


  19. #19
    Join Date
    Feb 2015
    Posts
    185
    Thanks
    5
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: multi threading or QConcurrent for the scenario

    I have made it re-entrant and it's working now for QtConcurrent::mapped(). Thanks

    But now I realise that what I am doing in two steps using mapped() can be done in single step using mappedReduced().

    When I try to implement getting the below error again

    error: no matching function for call to 'mappedReduced'
    QFuture<QString> decryptedContent = QtConcurrent::mappedReduced(fileList,DecryptMap('A '),ReduceS());
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~
    Qt Code:
    1. struct DecryptMap {
    2. DecryptMap(const QChar &key) : m_key(key) {}
    3. typedef QString result_type;
    4. QString operator()(const QString &item)
    5. {
    6. Decrypt *decrypt = new Decrypt();
    7. qDebug()<<item;
    8. return decrypt->decryptFile(item,'A');
    9. }
    10. QChar m_key;
    11. };
    12. struct ReduceS
    13. {
    14. typedef QString result_type;
    15. void operator()(QString & res, const QString & partial )
    16. {
    17. res.append(partial);
    18. }
    19. };
    20. QFuture<QString> decryptedContent = QtConcurrent::mappedReduced(fileList,DecryptMap('A'),ReduceS());
    21.  
    22. QString Decrypt::decryptFile(QString sourceFile,QChar keystring)
    23. {
    24. QString decryptedContent;
    25. decryptedContent.clear();
    26. string srcFile=sourceFile.toUtf8().constData();
    27. ifstream ffin;
    28.  
    29. ffin.open(srcFile.c_str(),ios_base::in|ios_base::binary);
    30.  
    31. char keyToEncryption= keystring.toLatin1();
    32. if(ffin.is_open())
    33. {
    34. char ch;
    35. while(!(ffin.read(&ch,1).eof()))
    36. {
    37. ch ^= keyToEncryption;
    38. decryptedContent.append(ch);
    39. }
    40. }
    41. ffin.close();
    42. return decryptedContent;
    43. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by ejoshva; 11th March 2015 at 04:43.

  20. #20
    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: multi threading or QConcurrent for the scenario

    Did you remember about proper includes?
    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. Multi-threading
    By plopes21 in forum Qt Programming
    Replies: 6
    Last Post: 2nd January 2013, 17:45
  2. Slow Multi-Threading
    By AwDogsgo2Heaven in forum Qt Programming
    Replies: 5
    Last Post: 19th July 2009, 21:36
  3. Multi-threading
    By lixo1 in forum Qt Programming
    Replies: 5
    Last Post: 22nd June 2009, 13:22
  4. Multi threading ...
    By kiranraj in forum Qt Programming
    Replies: 2
    Last Post: 18th June 2007, 16:51
  5. Multi-threading
    By shamik in forum Qt Programming
    Replies: 15
    Last Post: 28th November 2006, 10:22

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.