Solved QFile, QThread, QConcurrent and how to Qt
-
@A-Newbie
If I may suggest a git repo of mine:
https://github.com/DeiVadder/QtThreadExampleIn that project I do a threading task in each of the "Qt"-ways
The related functions are numbered.May help you further in your quest for knowledge 😉
-
Hello again,
@jsulm
I am not sure if I want to use many threads(among other things) to write to the same file, which is the reason I am asking how performance tools achieve this exactly. Many data IO performance bench tools have an option called "overlapped IO and IO depth", "concurrent IO count", "Outstanding IO" or something of the sort. In either case increasing the IO numbers to something adequate, say 3 or 4 brings about better read/write performance results from a single file, while a single-threaded sequential IO cannot do that.
I can probably simulate the same performance results if I shoot multiple threads reading from or writing to multiple files, but I am struggling at this point to understand how to do it with a single file.As for the new thread and timer, while I am aware of the parent-child relationship, but for some reason this timer wasn't working before I did that. Might have been just a coincidence in case I had my Qt::Connection was set to Queued instead of Direct at the time, or messing something some signals/slots up, or something of the sort, but I did spend the whole night reading forums as my timer wasn't working if moved to a different thread.
The reason I didn't pass my class as a parent to my timer is because of the Thread Affinity explained here: https://doc.qt.io/qt-5/qobject.html assuming I got the explanation there right of course =)
I can swear I didn't touch my signals/slots and tried Queued vs Direct connection every time I was changing something else.
In either case by all means it seems I goofed up something so I will settle with that, as it explains why I couldn't explain what happened there ^_^@J-Hilk
Thanks a bunch, having some good examples really means a lot to me! It gave me some good insight, but I mean to ask about the subclassing from QThread method. I read a lot of posts implying this is not the best method to handle threads. Is this because you have to be more wary of where and how you create things when you inherit from QThread in general and people struggle with this part, or is it something else?Also there is something which @SGaist said and has been bugging me:
"In any case, you would usually not invoke QtConcurrent code from a separate thread since QtConcurrent provides a high level set of functions that does the threading for you."
Besides the obvious reason, namely taking one additional thread(more resources) for the task what are the downfalls here? Is it something in general or more of a Qt thread methods/technology related?Any clues about why I had to use two different signal/slot connection methods?
-
@A-Newbie said in QFile, QThread, QConcurrent and how to Qt:
Any clues about why I had to use two different signal/slot connection methods?
What exactly did not work with the other approach?
-
@A-Newbie said in QFile, QThread, QConcurrent and how to Qt:
mean to ask about the subclassing from QThread method. I read a lot of posts implying this is not the best method to handle threads. Is this because you have to be more wary of where and how you create things when you inherit from QThread in general and people struggle with this part, or is it something else?
Well in general it is very easy to make something wrong. You have to have an fundamental understanding of Qt Object, &- Parent/Child system Threadaffinity in combination to the caller etc.
If you do an endless loop you'll have to implement your own check & abort functionality and if you call exec() you might as well do the Worker approach.
Besides the obvious reason, ..., what are the downfalls here
QtConcurrent provides, amongst other things, an integrated ThreadPool management, which is nice, but the whole thing comes with an overhead, so much in fact, that QtConcurrent is its own module, whereas QThread is part of QtCore
Any clues about why I had to use two different signal/slot connection methods?
care to elaborate?
-
-
@A-Newbie start is overloaded, it excepts an int and a void parameter
The docs actually have an example for exactly this one,
https://doc.qt.io/qt-5/qtimer.html#detailson how to correctly call the overload in connect
QTimer *timer = new QTimer(this); connect(timer, &QTimer::timeout, this, QOverload<>::of(&AnalogClock::update)); timer->start(1000);
-
@J-Hilk
Thanks again, I got it going with QOverload<>::of , but I am confused. Since I am passing it a void signal shouldn't it automatically choose the void version of the slot as normal overloads do?
Do the macro version, which I actually used do some sort of additional checks and adjustments and it works fine, or was it just luck it worked out for me and it chose the correct slot version? -
@A-Newbie
Hi
The difference between new syntax (the one needed overload )
and SIGNAL/SLOT macros is how that the new syntax works compile-time and the old
macro-based , works runtime.
The new syntax is based on pointers and needs full type info, the
old Macro version looks up the names at runtime.more info:
https://wiki.qt.io/New_Signal_Slot_SyntaxYou can see why the new syntax needs the QOverload in cases where there is
signals with the same name but different parameters. like int and string. -
@mrjj
Hi and thanks, this was an eye opener as I had no idea about the whole overloading thing. Never met it in any of the learning materials I went through so far.Regardless, I got answers pretty much to all of my questions and everyone helped a lot. Is there a way to simply have this topic as solved without picking a specific answer?
-
@A-Newbie said in QFile, QThread, QConcurrent and how to Qt:
Regardless, I got answers pretty much to all of my questions and everyone helped a lot.
great 👍
Is there a way to simply have this topic as solved without picking a specific answer?
sure bottem right side, topic tools button, simply set it to solved there