How to not interrupt back ground thread and also allow foreground ui function to get executed ?
-
i have implemented one ui . where on button click some ticket creation process happen.
In same program i have implemented background thread using mutex which send and receive this ticket creation related details to and from server.
So i need help regarding How to not interrupt back ground thread and also allow foreground ui function to get executed ?
-
@Qt-embedded-developer said in How to not interrupt back ground thread and also allow foreground ui function to get executed ?:
i want to stop background thread run time by setting priority for foreground ui function .
As already said, I would not assume that even if you change a foreground thread's priority this would "stop" any background threads. Not to mention, the moment the foreground thread makes any call such as I/O it's likely to yield to the background thread running, even ignoring pre-emptive multitasking.
and how to stop and continue this thread runtime when foreground process create ticket .
Standard threads tend to have "pause" and "resume" functions, but I don't see those in
QThread
. Instead it does haveQThread::requestInterruption()
&QThread::isInterruptionRequested()
, but these require you to write your code around them. -
@Qt-embedded-developer
Same sort of answer as in your other thread about this code you are writing.How to not interrupt back ground thread
Interrupt by what? And I really don't think you're supposed to be trying to write code like this.
and also allow foreground ui function to get executed
That's what should be happening automatically if you do other stuff in another thread, it's the behaviour you would expect from multiple threads.
Also, if I understand right, on the one hand you want to background thread not to get interrupted by the UI thread, and on the other hand you do want it to get interrupted so that the UI is responsive....
-
@Qt-embedded-developer
"yes" what? That you want the thread both to be interruptible and uninterruptible? -
@Qt-embedded-developer said in How to not interrupt back ground thread and also allow foreground ui function to get executed ?:
i want to stop background thread run time by setting priority for foreground ui function .
As already said, I would not assume that even if you change a foreground thread's priority this would "stop" any background threads. Not to mention, the moment the foreground thread makes any call such as I/O it's likely to yield to the background thread running, even ignoring pre-emptive multitasking.
and how to stop and continue this thread runtime when foreground process create ticket .
Standard threads tend to have "pause" and "resume" functions, but I don't see those in
QThread
. Instead it does haveQThread::requestInterruption()
&QThread::isInterruptionRequested()
, but these require you to write your code around them. -
Hi,
The question is: why do you want these interruptions in the first place ? What is the rational for that idea ?
-
@SGaist Because i have to allow ticket creation at highest priority while background data sending to server need to be done at lowest priority.
For example in bus ticket created by conductor. we can not stop conductor to create ticket. we can only interrupt the background thread to send that data to server.
So interruption needed at first place.
-
But that's the main idea of threads: there is no need to interrupt anything! Your foreground and background threads can run in parallel, both doing their job at the same time.
-
@sierdzio Hi !!
But there is one situation make my sqlite database get locked .
Because my back ground thread and foreground function use mutex lock that make my sqlite db to get locked . and i want to stop locking of database.
So How to deal with this situation ?
-
@Qt-embedded-developer said in How to not interrupt back ground thread and also allow foreground ui function to get executed ?:
So How to deal with this situation ?
You expect to get clear answer without showing your code? Especially how you lock.
Here some hints: https://en.wikipedia.org/wiki/Deadlock#Prevention -
@Qt-embedded-developer said in How to not interrupt back ground thread and also allow foreground ui function to get executed ?:
one resource sqlite database
Before commenting on what you have said, could you clarify: do you mean a SQLite database file which is supplied and opened as a Qt resource file, or do you mean "resource" in a broad sense and you are just talking about an external SQLite file, not a Qt resource?
-
Sounds like the solution would be to keep the SQLite DB opened by only one thread, which should in turn distribute the data as necessary to other parts of the application, for example using signals and slots. No locking and no mutexes :-)
BTW. be very careful with SQLite and threads, by default it does not support them. You need to compile SQLite yourself. See https://sqlite.org/threadsafe.html
-
@sierdzio said in How to not interrupt back ground thread and also allow foreground ui function to get executed ?:
BTW. be very careful with SQLite and threads, by default it does not support them. You need to compile SQLite yourself. See https://sqlite.org/threadsafe.html
The link you provided states that by default SQLite uses serialized access, i.e. it is thread safe by default.
@Qt-embedded-developer You should not use mutexes to access SQLite. With the proper settings multiple connections to the same SQLite file can be open. SQLite will do all the synchronization. You might get into trouble if one of the threads reads quite often and the other thus cannot lock it for writing.
Maybe you can clarify what you are actually trying to do. You should not use a database to communicate between two threads (one polling if there is something new to be sent over the network). My guess is that you don't need mutexes at all. In order for the GUI thread to communicate that there is something new to be sent, just use signals and slots between the two threads to communicate. All requests from the GUI will be queued inside the event loop of the network thread and handled one after the other. This is how Qt should be used.