Qt Thread architecture
-
Hello Qt Gurus,
I have following process scenario. I need receive datas from server A, parse the content and put the datas to server B.
I itend to have two thread, the main thread with gui to communicate with server B and another thread communicate with server A. The following is pseudo-code
class ServerAThread: public QThread { QMutex _mutex; QString _data; QLocalServer _server; void run { QMutexLocker locker( &_mutex ); _server.receive( data ); _data = data; } public: ServerAThread( QString &data ) : _data( data ) {} }; class MainWindow : public QMainWindow { QString _data; // shared data between Thread A and Main ServerAThread _threadA; MainWindow() : _threadA( _data ) { _threadA.start(); } }Is this architechture great or do you have better idea for this? Do I need a loop to check any change of datas or is better to use emit signal? Do emit Signal block the thread A if the main thread need time to parse the content of data?
-
Hello Qt Gurus,
I have following process scenario. I need receive datas from server A, parse the content and put the datas to server B.
I itend to have two thread, the main thread with gui to communicate with server B and another thread communicate with server A. The following is pseudo-code
class ServerAThread: public QThread { QMutex _mutex; QString _data; QLocalServer _server; void run { QMutexLocker locker( &_mutex ); _server.receive( data ); _data = data; } public: ServerAThread( QString &data ) : _data( data ) {} }; class MainWindow : public QMainWindow { QString _data; // shared data between Thread A and Main ServerAThread _threadA; MainWindow() : _threadA( _data ) { _threadA.start(); } }Is this architechture great or do you have better idea for this? Do I need a loop to check any change of datas or is better to use emit signal? Do emit Signal block the thread A if the main thread need time to parse the content of data?
Hi,
The first question is: why do you think you need threads involved ? Qt as well as its networking classes are asynchronous. Take advantage of that first. If the load is so heavy that it triggers freezing issues in your GUI then, and only then, add a thread in the mix.
-
Hi,
The first question is: why do you think you need threads involved ? Qt as well as its networking classes are asynchronous. Take advantage of that first. If the load is so heavy that it triggers freezing issues in your GUI then, and only then, add a thread in the mix.
-
Hello, I forgot to mentions about communication with Server A using third libraries, so it need a loop, check in every loop the data is coming in. So there must a thread for the communication with Server A.
Well, QLocalServer is also asynchronous. Which 3rd party library do you need to use ? You are showing none in your code sample.
-
Hello, I forgot to mentions about communication with Server A using third libraries, so it need a loop, check in every loop the data is coming in. So there must a thread for the communication with Server A.
-
its a tdlib on github, from its I take datas, it runs with a loop, check its state and give the content to main thread to parse the content and forward to Server B
The provided code does nothing - at least what not you expect. It neither shares some data nor is the mutex useful at all. Receive the data, emit a signal and connect it to a slot in your main thread if you need a separate thread at all - your example code does not.
-
The provided code does nothing - at least what not you expect. It neither shares some data nor is the mutex useful at all. Receive the data, emit a signal and connect it to a slot in your main thread if you need a separate thread at all - your example code does not.
tdlib ? Are you writing a client for telegram ? If so, there are Qt based libraries for that which might be simpler to use in your case.
-
It is not good style (anymore) to override
QThread::run(). Have a look at the documentation for QThread how to create a worker thread.There is an even better approach for your use case. Just create a plain old QThread object and start it. It will run its own event loop. You should probably not run an infinite loop to takes up every CPU cycle of one core. Instead, start a QTimer (with a low timeout) in this thread to call your external library and check for new data.
Also, try to avoid QMutex. Synchronization between threads is slow. Furthermore, in your small code example only a single object has access to the mutex. This means that you don't need it.
-
The provided code does nothing - at least what not you expect. It neither shares some data nor is the mutex useful at all. Receive the data, emit a signal and connect it to a slot in your main thread if you need a separate thread at all - your example code does not.
Really? Could you please provide me the name or github link?
-
Really? Could you please provide me the name or github link?
-
It is not good style (anymore) to override
QThread::run(). Have a look at the documentation for QThread how to create a worker thread.There is an even better approach for your use case. Just create a plain old QThread object and start it. It will run its own event loop. You should probably not run an infinite loop to takes up every CPU cycle of one core. Instead, start a QTimer (with a low timeout) in this thread to call your external library and check for new data.
Also, try to avoid QMutex. Synchronization between threads is slow. Furthermore, in your small code example only a single object has access to the mutex. This means that you don't need it.
@SimonSchroeder said in Qt Thread architecture:
It is not good style (anymore) to override
QThread::run(). Have a look at the documentation for QThread how to create a worker thread.There is an even better approach for your use case. Just create a plain old QThread object and start it. It will run its own event loop. You should probably not run an infinite loop to takes up every CPU cycle of one core. Instead, start a QTimer (with a low timeout) in this thread to call your external library and check for new data.
Also, try to avoid QMutex. Synchronization between threads is slow. Furthermore, in your small code example only a single object has access to the mutex. This means that you don't need it.
Thx you, I have a infinite run loop, that need to check the state of the connection and data received. In this case is movetoThread also possible or override QThread::run is best fot fit?
-
Really? Could you please provide me the name or github link?
@king558 said in Qt Thread architecture:
Really? Could you please provide me the name or github link?
I don't want to nitpick but you literally tagged @Christian-Ehrlicher with that request hence my question.
Anyway, the first hit when searching Qt telegram client brings you this https://github.com/Kaffeine/telegram-qt
-
@king558 said in Qt Thread architecture:
Really? Could you please provide me the name or github link?
I don't want to nitpick but you literally tagged @Christian-Ehrlicher with that request hence my question.
Anyway, the first hit when searching Qt telegram client brings you this https://github.com/Kaffeine/telegram-qt
-
K king558 has marked this topic as solved on