Note that there are some explanatory texts on larger screens.

plurals
  1. POExplanation for behaviour of code
    primarykey
    data
    text
    <p>I was following the multithreading tutorial from <a href="https://computing.llnl.gov/tutorials/pthreads" rel="nofollow">https://computing.llnl.gov/tutorials/pthreads</a> and experimenting with some of the code provided.</p> <p>I used this source file <a href="https://computing.llnl.gov/tutorials/pthreads/samples/hello.c" rel="nofollow">https://computing.llnl.gov/tutorials/pthreads/samples/hello.c</a> then added this to the main function:</p> <pre><code> void * v; v = (void *)t; </code></pre> <p>and replaced this line:</p> <pre><code>rc = pthread_create(threads+t, NULL, PrintHello, (void*)t); </code></pre> <p>by this:</p> <pre><code>rc = pthread_create(threads+t, NULL, PrintHello, v) </code></pre> <p>Arguably(I know it's not a good argument :) ), the output should have remained unchanged.. but this is the new output:</p> <pre><code>In main: creating thread 0 In main: creating thread 1 Hello World! It's me, thread #140734232544832! In main: creating thread 2 In main: creating thread 3 Hello World! It's me, thread #140734232544832! In main: creating thread 4 Hello World! It's me, thread #140734232544832! Hello World! It's me, thread #140734232544832! Hello World! It's me, thread #140734232544832! </code></pre> <p>Thread # is garbage! </p> <p>Can someone explain to me what's going on here? Why is the output changed?</p> <p>Is it because t was being passed by value and casted while being passed to PrintHello while now, after the change I am attempting to pass pointer and while the address of that pointer is passed properly - that address does not contain the value t contains since that was local to main?</p> <p>Can someone please confirm/ reject and fix my theory?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload