Results 1 to 2 of 2

Thread: CodeFinder

  1. #1
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default CodeFinder

    Hi!

    Thought I'd share a small utility, I recently wrote. It loads all text-files from a given directory into memory and lets you search for text or regular expressions. Both loading and searching are multi-threaded. Takes around 30secs to load all qt src files on my machine. I took the code editor from the qt-examples. Added some highlighting rules.

    CodeFinder-OptionTab.pngCodeFinder-SearchTab.png

    For the multi-threading, I ended up using the following construct. Maybe that helps somebody else too.

    Qt Code:
    1. class ThreadPoolTask : public QObject, public QRunnable
    2. { Q_OBJECT
    3. signals:
    4. void start();
    5. public:
    6. ThreadPoolTask(QThreadPool* tp) {threadPool = tp;}
    7. protected:
    8. void run() {
    9. threadPool->tryStart(this);
    10. emit start();
    11. }
    12. private:
    13. QThreadPool* threadPool;
    14. };
    To copy to clipboard, switch view to plain text mode 

    This tasks start signal is then to be directly connected to some member slot, which is executed in parallel as often as QThreadPool sees fit. This way you can easily and savely interrupt the process and use signals during the process. You just need to protect critical sections with mutexes or semaphores.

    Qt Code:
    1. QThreadPool tp;
    2. ThreadPoolTask* task = new ThreadPoolTask(&tp);
    3. connect(task,SIGNAL(start()),this,SLOT(doLoadFile()),Qt::DirectConnection);
    4. connect(task,SIGNAL(destroyed()),this,SLOT(doFinish()));
    5. tp.start(task);
    6.  
    7. while(dir_walker.hasNext())
    8. {
    9. dir_walker.next();
    10. ...
    11. if (found == true)
    12. {
    13. QMutexLocker ml(&fileNameMutex);
    14. fileNames.append(dir_walker.filePath());
    15. fileCount.release();
    16. }
    17. }
    18. fileCount.release(tp.activeThreadCount());
    19.  
    20. void TextFileLoader::doLoadFile()
    21. {
    22. forever
    23. {
    24. fileCount.acquire();
    25. QString fn;
    26. {
    27. QMutexLocker ml(&fileNameMutex);
    28. if (fileNames.count() == 0) return;
    29. fn = fileNames.takeFirst();
    30. }
    31. ....
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

    I found no way of setting the threadpools thread priority other than creating an empty wrapper thread with lower priority. The threadpool then inherits its priority. Is there another way?

    Any thoughts and criticism are greatly appreciated! And also let me know if you like the tool!

    Johannes
    Attached Files Attached Files

  2. #2
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: CodeFinder

    I uploaded a win32 build with shared qt libraries to qt-apps.org

    http://qt-apps.org/content/show.php/...content=135290

    Johannes

    PS: The source package above contains an unnecessary include for the boost library in the pro file.

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.