samedi 26 septembre 2020

How to transfer data between main function and threads?

I want to store the threads that I have created in my main function in an array and later access them in my Thread class. What would be the best way to do this?. Below is the structure of my code.

Main.cpp:

int main(){
//do something

while ((new_socket = socket.Accept())) {
        std::thread mythread(&Myclass::MyThread, &myclass, 
                std::move(new_socket), para1);
        // I want to store the above threads created in an array which can later be accessed in a different thread class
    }
}

MyClass.cpp

MyClass::MyThread(....){
I want to access the array of threads here.
}

I have tried mutex and cv and add these threads into a queue but it is generating many errors. What is the best way to solve this?

Aucun commentaire:

Enregistrer un commentaire