Home > Mobile >  Multi-threaded data synchronization problem, have the general method and make the UI to flow
Multi-threaded data synchronization problem, have the general method and make the UI to flow

Time:10-30

A simple example, for example, the UI read a structure, displayed, structure the data read from the serial port to come in,
So there are three roles, three threads,
1. The UI, the main thread,
2. The model, the child thread A,
3. The serial port, the child thread B,

At any time to write a serial port to model data, the model according to the custom protocol unpacking, plus private material and algorithm, it is a logic, and then put into the cache, and UI read data from the model at any time,
Then the model of reading and writing must be locked, then write a serial port and avoid model, is to write locks, UI read, it is the lock, the UI will be lock gets stuck, is there a general way to avoid the UI will not be card?

I tried in reading data interface of the model, try_lock, then QApplication: : the processEvent (), to let the user interface (UI) to run the event when don't get the lock, but found more card, and I don't know is what reason, because I am just a simple example above, actually my project is very large, there are many other threads characters will call read data interface of the model, is not only the UI thread,

CodePudding user response:

Why the UI thread to lock. Try_lock although I don't know why, but certainly time consuming, the timeout is returned said "on the lock is not" return values, many a mickle makes a muckle, and UI is card

To add a child no longer thread C reading model, when finish read want to display data, to update the data, tell the UI update

CodePudding user response:

reference 1st floor fly4free response:
why the UI thread to lock. Try_lock although I don't know why, but certainly time consuming, the timeout is returned said "on the lock is not" return values, many a mickle makes a muckle, and UI is card

Will no longer add a child thread C reading model, when finish read want to display data, update data, tell the UI update
you're right, should be to a role in the child thread to read model, and then sent to the UI,
  •  Tags:  
  • Qt
  • Related