Reputation: 175
I am trying to learn little bit about Multi-Threading because I need it in my bachelor thesis. I think I understand it on low teoretical level but not on practical.
For the beginning I am trying to create very easy program, where I would like to learn how to work with QThread. My small program test all files in directory if they are .txt files.
There is function which just test if one file contains .txt and its used in next functions. I know I know this is not perfect test for testing if file is .txt but it really doesnt matter. This is about something else.
void FileTester::testIfTxt(QString &file){
if( file.contains(".txt"))
std::cout << file.toStdString() << " is .txt file" << std::endl;
else
std::cout << file.toStdString() << " is NOT .txt file" << std::endl;
}
Here is function for testing without using multi-threading.
void FileTester::normalTest(){
QDir folderPath("D:\\programming\\C++\\multithreading-qt\\files");
QStringList filesList = folderPath.entryList(QDir::NoDotAndDotDot | QDir::Files);
auto t_start = std::chrono::high_resolution_clock::now();
for(QString& file : filesList){
testIfTxt(file);
}
auto t_end = std::chrono::high_resolution_clock::now();
double elapsed_time_ms = std::chrono::duration<double, std::milli>(t_end-t_start).count();
std::cout << elapsed_time_ms << " ms" << std::endl;
}
Now I would like to do the same but with multithreading. For the beginning we can use 3 threads. First thread would take first file and test, while second thread would take second file and test and third thread would take third file and test. When first thread will end work, than take nex untested file and test etc. .......
I started with function there but I really dont understand, how to use something like this. I started with something like this.
void FileTester::multiThreadTest(){
auto t_start = std::chrono::high_resolution_clock::now();
allMultiThreadTest();
auto t_end = std::chrono::high_resolution_clock::now();
double elapsed_time_ms = std::chrono::duration<double, std::milli>(t_end-t_start).count();
std::cout << elapsed_time_ms << " ms" << std::endl;
}
void FileTester::allMultiThreadTest(){
QThread* thread1 = new QThread();
QThread* thread2 = new QThread();
QThread* thread3 = new QThread();
thread1->start();
thread2->start();
thread3->start();
for(QString& file : filesList){
testIfTxt(file);
}
}
Again this is not code from my bachelor thesis. I just want to learn how to use multithreading on this easy program. Thank you for any help.
Upvotes: 1
Views: 846
Reputation: 181715
The QThread documentation, like most Qt documentation, is very good, but the code examples might be a bit overwhelming.
There are basically two ways to use QThread
:
Useful when you want to run some code on a thread, and exit the thread when the code finishes.
Create a subclass of QThread
and override its run()
function. The code you put in that function will run in the thread, and when the function returns, the thread exits.
To start the thread, simply create an instance of your subclass and call start()
on it.
Useful when you want to keep a thread in the background, and occasionally send it some work to do. This approach needs some boilerplate code because you need an object to emit a signal and another object to receive it with a slot.
Create a subclass of QObject
and give it a public slot of your choice.
Create an instance of QThread
(no subclass needed here). The default implementation of run()
contains an event loop, which simply waits for signals and runs the corresponding slots, just like QApplication::exec()
does for your main thread.
Create an instance of your QObject
subclass. Move the latter to your new thread using QObject::moveToThread()
. The effect of this is, that any slots being called by signals will be run on that thread, rather than the main thread. See thread affinity and queued connections.
Connect the slot to a signal that is emitted by any QObject
subclass on your main thread.
To send work to the thread, simply emit that signal.
Note: any arguments that you pass to the signal must be known to Qt's meta-type system. Most built-in Qt types are okay. If you run afoul of this rule, you will get a warning at runtime.
Upvotes: 3