Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error passing volatile std::queue<int> discards qualifiers [-fpermissive] C++98

Tags:

c++

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

  • Use static volatile to declare global variables for sharing between functions running in different cores.
like image 344
STiGMa Avatar asked Jul 12 '15 11:07

STiGMa


2 Answers

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.

like image 130
David Schwartz Avatar answered Oct 12 '22 06:10

David Schwartz


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.

like image 30
Kerrek SB Avatar answered Oct 12 '22 05:10

Kerrek SB