What should be best mechanism to stop and start same thread again?
-
I have created one class
Which shared between both
This class have one queue
Using this class I have created 2 queue
In one of that queue frontend will write data using safeguard lock and it will be read by backend
In other queue back end will write data and front will read data
Now to not block GUI I have created 2 thread on front end side where one will do write and other do reading
What should be best mechanism to stop and start same thread again so that when data is not available at that time we can save usage of cpu?
In this queue
I want to push the one command
Which have
Struct {
Int id;
String type;
// here I am confused what I should declare so that there any number of data I can pass
}So what I should declare 3rd var?
-
Hi,
Isn't QWaitCondition what you are looking for ?
Check the associated producer consumer example. -
@SGaist what about
My third variable to declare? Where I mentioned below comment
// here I am confused what I should declare so that there any number of data I can passI want to share here information it can be any type of information
-
@Qt-embedded-developer "any number of data" ? What is the type of data ? Once you know that, you can use a
QList<TypeOfData>
. -
@Qt-embedded-developer said in What should be best mechanism to stop and start same thread again?:
So what I should declare 3rd var?
This says it all... Since it's C++ (and not something like JS, where you can have "variable Varibales") , you need to know what datatype you put there before you compile/run it.
In general one could use<T>
templates or void pointers to your "data" but in your case , in a struct it might be difficult.
So there is no way that you know before what kind of data is stored there?! Why you not know? Re-think your design. Or create different structs for different cases -
@Qt-embedded-developer
One Qt type which approximates JSON's variety of types isQVariant
/QVariantList
etc. I don't know what your case is where you do not define the type/structure of your data.QList<TypeOfData>
might be preferable, butQVariant
allows for a number of underlying types in answer to your question. -
@Qt-embedded-developer JSON is a textual exchange format which C++ is not. You can't create new data structure out of the blue without any prior knowledge like that.
What exactly are you trying to put in that structure ?
-
My actually requirement is like
Consider in ui there are several button for different devices
Now when any button get clicked I need to send request through this type of data structure so when response is send by backend
Based on devices properties there could be more or less data in various format comes.
And this type of data I need put to send and receive
-
@Qt-embedded-developer said in What should be best mechanism to stop and start same thread again?:
i know that using json format we can send as many as various types of data. So similar way I want to create structure that replace json format
So going back to this. If you really do want to pass a bunch of unknown, arbitrary data structures around JSON is a possibility. Why do you want to "replace" JSON format?
-
@Qt-embedded-developer what is the justification of that requirement ?
You can make use of Qt's JSON classes to parse and generate from / to the backend.
-
@Qt-embedded-developer
If that is your requirement then you can always restrict yourself to the same subset of types as JSON does. From Qt useQList<>
for lists/arrays (JSON[...]
) andQMap<>
for objects with keys (JSON{...}
). Or equivalentstd::
types if that floats your boat. -
-