mercredi 8 avril 2020

How do I create different number of threads in c++?

In my program, I want to get number of threads from user. For example, user enters number of threads as 5, i want to create 5 threads. It is only needed in the beginning of the program. I don't need to change number of threads during the program. So, i write the code such as;

int numberOfThread;

cout << "Enter number of threads: " ;
cin >> numberOfThread;

for(int i = 0; i < numberOfThread; i++)
{
    pthread_t* mythread = new pthread_t;
    pthread_create(&mythread[i],NULL, myThreadFunction, NULL);
}

for(int i = 0; i < numberOfThread; i++)
{
    pthread_join(mythread[i], NULL);
}

return 0;

but i have an error in this line pthread_join(mythread[i], NULL);

error: ‘mythread’ was not declared in this scope.

What is wrong in this code? and do you have a better idea to create user defined number of thread?

Aucun commentaire:

Enregistrer un commentaire