C - fields in struct used between threads
<p>I start a thread and pass to its entry point a struct which has had some values initialized. The pointer I get is correct, and I can get the pointer to a particular field in that struct, but the value I see there is not what I originally set it to be. How does that happen?</p>

<p>Example:</p>

<pre><code>typedef int (*CNX_Algorithm)(void*, int);

typedef struct
{
    CNX_Algorithm algorithm;
    thrd_t thread;
    void *data;
    int go;
} CNX_Instance;

int RunContinuously(void *instance)
{
    int count = 0;
    int *go = &amp;((CNX_Instance*)instance)-&gt;go;
    CNX_Algorithm algorithm = ((CNX_Instance*)instance)-&gt;algorithm;
    void *data = ((CNX_Instance*)instance)-&gt;data;
    printf("new thread: %p %p %p\n", instance, &amp;((CNX_Instance*)instance)-&gt;data, data);
    while(*go) count += algorithm(data, 1);
    return count;
}

CNX_Instance *CNX_StartInstance(CNX_Algorithm algorithm, void *data)
{
    CNX_Instance *instance = malloc(sizeof(CNX_Algorithm));
    instance-&gt;algorithm = algorithm;
    instance-&gt;data = data;
    printf("original thread: %p %p %p\n", instance, &amp;instance-&gt;data, instance-&gt;data);
    instance-&gt;go = 1;
    thrd_create(&amp;instance-&gt;thread, RunContinuously, instance);
    return instance;
}
</code></pre>

<p>Output:</p>

<pre><code>original thread: 024b1f78 024b1f84 0019ff10
new thread: 024b1f78 024b1f84 80000008
</code></pre>

<p>Huh???</p>

<p>EDIT:</p>

<p>I should mention that if I CALL RunContinuously instead of starting a thread there it works as expected. Go figure.</p>