Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am learning c++ and came across below code in which I did not understand below line.
rc = pthread_create(&threads[i], NULL, PrintHello, (void *)&td[i]);

Where,
rc = return code in int
thread is pthread_t threads[5]
PrintHello is threadRoutine
td is structure with 2 variable 1 is int thread_Id and other is char* msg

I did not understand why (1st argument) is passed as a reference and (last argument) td typecasted to (void*)

Complete code:
struct thread_data {
int thread_id;
char *message;
};

void *PrintHello(void *threadarg) {
struct thread_data *my_data;
my_data = (struct thread_data *) threadarg;

cout << "Thread ID : " << my_data->thread_id ;
cout << " Message : " << my_data->message << endl;

pthread_exit(NULL);
}

int main () {
pthread_t threads[NUM_THREADS];
struct thread_data td[NUM_THREADS];
int rc;
int i;

for( i = 0; i < NUM_THREADS; i++ ) {
cout <<"main() : creating thread, " << i << endl;
td[i].thread_id = i;
td[i].message = "This is message";
rc = pthread_create(&threads[i], NULL, PrintHello, (void *)&td[i]);

if (rc) {
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
}
pthread_exit(NULL);
}

What I have tried:

Read references but did not understand why it is necessary here
Posted
Updated 23-Jun-19 23:19pm

In the below line
C++
pthread_create(&threads[i], NULL, PrintHello, (void *)&td[i]);
the first argument is not passed by reference (in C++ you must NOT use the & operarator in a call by-reference). It is instead the address (i.e. pointer to) of a pthread_t variable (namely it is the address of the threads[i], that is the ith item of the array threads) as required by the pthread_create[^] function.
 
Share this answer
 
Comments
k5054 23-Jun-19 11:26am    
At the risk of confusing the OP, I'd argue that the first argument is, indeed, "passed by reference", but not "passed as a C++ reference". The term "passed by reference" in Computer Science is used to indicate when an argument is passed by pointer, and so is modifiable in the subroutine. This contrasts with"passed by value", where a copy of the value is passed in, and any changes made in the subroutine are not reflected in the caller.
CPallini 23-Jun-19 12:13pm    
In C++, all the is not passed by (C++) reference is passed by value. The first argument is a pointer passed by value.
Richard MacCutchan 24-Jun-19 5:44am    
I agree with Carlo. The two parameters are passed by pointer to address, not by reference.
I agree 100% with CPallini. But in order to help the OP I would like to clarify like this:

Whether an argument is passed by reference or value is determined ONLY by the the function declaration NOT the function call. If and only if you have a "&" in the function declaration: the argument is passed by reference.

Answering the broader question "why is there a & in this call?" Your code would not compile (try it!) without it since pthread_create() requiers a pointer to a pthread_t type.
pthread_create(3) - Linux manual page[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900