lundi 5 septembre 2022

How to transfer a mutex containing object from one function to another?

#include <mutex>

class ConcurrentQueue {
    // This class contains a queue and contains functions push to the queue and pop from the queue done in a thread safe manner.
    std::mutex m;
};

class Producer {
    // This class contains several methods which take some ConcurrentQueue objects and then schedule tasks onto it.
public:
    void func(ConcurrentQueue a) {}
};

class Consumer {
    // This class contains several methods which take the same ConcurrentQueue objects and then remove the tasks and complete them one by one.
public:
    void func(ConcurrentQueue a) {}
};

int main() {
    // Here I want to generate the necessary ConcurrentQueue objects and then start threads for producer and consumer methods where I supply it with the required queue objects.
    ConcurrentQueue a;
    Producer b;

    // :( Unfortunately I cannot pass in any of my ConcurrentQueue objects to the methods as apparantly I cannot copy transfer a mutex.
    b.func(a); // This line gives compiler error saying the copy constructor is deleted.
    
    return 0;
}

The above code explains the whole situation through comments. How do I design it better so that I am able to achieve this?

Aucun commentaire:

Enregistrer un commentaire