-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggestion on PThreads (passing argument, example3) #13
Comments
I can create a PR if this suggestion is accepted. Thanks! |
Hi, the author wrote this example (marked as Incorrect) meant to show us that passing an local variable as thread's argument is |
Also, your modification seems not right, because you pass an local variable t to every thread while adding t by 1, according to the context, we should passing every thread a t, not every thread shares one value. If you want to write the correct version, here's a modification int main() {
pthread_t threads[NUM_THREADS];
int rc;
long t[NUM_THREADS];
for (t = 0; t < NUM_THREADS; t++)
{
printf("In main: creating thread %ld\n", t);
t[i] = i;
rc = pthread_create(&threads[t], NULL, PrintHello, &t[i]); /* you don't need to add (void*) */
if (rc)
{
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
/* Last thing that main() should do */
pthread_exit(NULL);
return 0;
} |
As from the title, in PThreads tutorial, Passing Arguments to Threads, Example 3
with output
It looks like the program prints the address of the variable
t
, which might be inconsistent with what we want to illustrate --- "the value of this memory location changes". Can we adjust this to something like the following?So the output goes like
which illustrates that the value
t
had beent++
ed before the created thread read the location.Thanks!
The text was updated successfully, but these errors were encountered: