I have a
volatile std::queue<int> requestQueue;
and when I try to call within a function any of its methods (pop, push, empty, front etc), e.g.:
while (!requestQueue.empty()){
...do something
}
I get the following error.
robot.cpp:43:31: error: passing 'volatile std::queue<int>' as 'this'
argument of 'bool std::queue<_Tp, _Sequence>::empty()
const [with _Tp = int, _Sequence = std::deque<int, std::allocator<int> >]'
discards qualifiers [-fpermissive]
I found online that a typecast might be needed, but I am not sure whether this is the case.
Any ideas? Thanks in advance.
Update
1) The program uses such a data structure to store a sequence of requests so that a server can take the requests at its own service rate.
2) volatile is used to support data sharing between functions running on different cores.
3) The documentation of the library I use states the following regarding the use of
You're expecting volatile
to work impossible magic. It is simply the case that std::queue
is not a thread-safe queue, nor is it a waitable queue. You can make a thread-safe, waitable queue if you want, but just attaching volatile
to std::queue
won't magically conjure the code necessary to do that out of nowhere.
This is a common misunderstanding about what volatile
does. It some cases, on some platforms, it happens to do what you happen to need to make some things work between cores. But that's always by a combination of accident and luck. It's almost never the right way to synchronize across cores in C or C++.
I suspect that your real question is, "How do I make a thread-safe, waitable queue in C++?" And the answer is to use a std::mutex
to make it thread-safe, and a std::condition_variable
to make it waitable. These things are not simple to use, and some expertise is needed, so you may prefer to find a suitable queue that is already written.
If your code isn't C++11, then we'll need to know how you're creating threads to know how you should be synchronizing them. Windows threads has ways, POSIX threads has ways. If this isn't threads (maybe it's processes sharing memory or something like that) then std::queue
is not even remotely close to what you need -- it can't possibly work because std::queue
uses pointers internally, and those won't work across processes.
The queue member functions are not volatile-qualified, so you cannot use them on volatile queue instances. Basically, if you must have something volatile, then this is not the data structure you are looking for.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With