Unsolved what is the best way to update UI by data(got from the non-mainThread)?
-
what is the best way to update UI by data(got from the non-mainThread)?
the data type is not basic, but a struct.i know 2 ways:
- write a thread-safe class with SetData & GetData
- pass the Struct as param by connect(), from sender(noo-mainThread) to reciever(mainThread). i know it can be done by use : Q_DECLARE_METATYPE, qRegisterMetaType
-
which method is more efficient?
1st way should add mutex;
2nd way is pass by value; -
@opengpu
Whatever the data, if it's in a non-GUI thread you will want to define a signal to send it to the GUI thread and a slot in the GUI thread to retrieve it and do whatever to the GUI.You'll want to think about the connection type. See https://doc.qt.io/qt-5/threads-qobject.html, Signals and Slots Across Threads. And you'll want to think about the lifetime/ownership of the data structure you're passing, since it's across threads.
-
which method is more efficient?
1st way should add mutex;As a general statement: using a mutex across threads will cause synchronous behaviour: one thread gets blocked while the other reads/writes. Using signals/slots will, at least in theory, allow the scheduler to decide when best to allow each thread to run independently, without blocking. Whether this makes much difference in practice with all the various overheads going on is a different matter.
-
@opengpu I suggest to use signals/slots as this is tested and proven concept and you do not have to do anything special. Synchronising threads manually can be tricky and error prone task.
-
@jsulm thank you .
and how is the speed, as i have to emit a not small Struct across threads -
@opengpu Depends on the size of the struct and how often you emit the signal
-
emit very frequently, but the struct is not very big
-
is custom Struct used as param of connect effiecnt?
-
@opengpu said in what is the best way to update UI by data(got from the non-mainThread)?:
is custom Struct used as param of connect effiecnt?
totally depends on the efficiency of your copy constructor of your struct
-
@J.Hilk thank you.
but as signal and slot are not in the same thread. is there any other way except these 2 method?- pass Struct as value;
- SetData() {mutex; m_data = data;} @the signal thread
GetData() {mutex; return m_data;} @the slot thread
-
@opengpu How big is your struct and how often do you emit the signal?
-
@jsulm ```
class Data
{
public:
VARIANT data1;
VARIANT data11;
VARIANT data111;
VARIANT data1111;
LONG errorCode;
public:
Data();
~Data();
};maybe emit every second, or even more frequently
-
is this a good way to emit signal in the non-thread non-qt Callback function?
WINAPI Callback(const Data& data) {//here is not mainThread; so i have to pass the data by value to the slot which is in mainThread; or i use mutex & save data to m_data, and then in slot mainThread getData also with mutex protect thread-safety g_qtObj->emit mySignal(); } class QtObj : public QObject { Q_OBJECT signals: void mySignal(); } QtObj g_obj; QtObj* g_qtObj = &g_obj;
-
@opengpu And how big is VARIANT?
1HZ isn't something I would care. In such a case I would exchange the data via signals/slots between the threads instead of messing up with threads synchronisation. -
-
-
@J.Hilk windows10 & x64
-
ok, so i will pass it by-value in connect & across theads
-
my exe recieve data from internet then update and show the data realtime, such as stock market realtime price changing very fast.