ameerosein ameerosein - 1 year ago 89
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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download