As the question suggested, I have a vector or a list of queues
template <typename T>
class queue {
private:
std::queue<T> m_queue;
std::mutex m_mutex;
public
void push(T item);
T pop();
}
std::vector<queue<DefinedDataStructure>> QueueList;
std::list<queue<DefinedDataStructure>> QueueList2;
Is it possible to mutex a lock a single element like QueueList[0], which is also a queue?
If not, could you please suggest a way how I can link the individual queues together and still able to lock some of them when necessary?
I appreciate all the ideas.
What is
queue
in this example?std::queue
does not have a mutex lock. If you need that, you have to add it yourself, such as by storing astd::queue
and astd::mutex
in astruct
orclass
. Please clarify your situation.@RemyLebeau Let me clarify. Queue is a thread-safe class implementation of queues. “` template <typename T> class queue { private: std::queue<T> m_queue; std::mutex m_mutex; “` I’m wondering if I lock down a
queue
object, would it make the whole vector container inaccessible to other threads?No, it would not. You would need a separate
mutex
for thevector
itselfYou could make a threadsafe vector in the same way. Make a class with a mutex like your queue class and expose methods on that new class you need. And in the implementation use
std::scope_lock lock{mutex}
. (do NOT inherit from std::vector it is not designed to be inherited from).Note that you cannot create a vector with your
queue
as its value type, since its member variable of typestd::mutex
does provide neither copy constructor nor move constructor. Relevant question: stackoverflow.com/q/16465633/580083.