ameerosein ameerosein - 6 months ago 26
C++ Question

passing a single integer to a multithread function in C++

I want to call a function with multi threads, and i only need to pass a single integer to that function (the thread id, so if it is accessible i need no value to pass).

how should i do this?

for example like:

for(int i=0; i < numberOfThread; i++ ){
pthread_create(&threads[i], NULL, multichaper, &td[i]);

in which multichaper is my function and threadID is and integer.


General approach to this is to make the function like this:

void* multichaper(void* arg)  {
    int tid = *(int*)arg;

And while calling pthread_create:

pthread_create(&threads[i], NULL, multichaper, &td[i])

where td[i] is an int.