Note that there are some explanatory texts on larger screens.

plurals
  1. POpthread_create function format and pointers - C Linux POSIX library
    primarykey
    data
    text
    <p>The question I have is, what exactly is the format of the pthread_create function and the function it calls, in terms of pointers and such? I can wrap my head around variable pointers, although I need to clarify my knowledge in that area, but function pointers get icky.</p> <p>I understand the preferred format is</p> <pre><code>void *threadfunc(void *arg); int main() { pthread_t threadinfo; int samplearg; pthread_create(&amp;threadinfo, NULL, threadfunc, &amp;samplearg); } </code></pre> <p>However, this generates a compiler warning that threadfunc's not returning a value, so apparently the * is something about what threadfunc returns, not a characteristic of the function?</p> <p>I have also seen the function defined as and the pthread_create formatted like such:</p> <pre><code>void threadfunc(void *arg); pthread_create(&amp;threadinfo, NULL, (void *)threadfunc, &amp;samplearg); </code></pre> <p>Which of these two are correct, or are they functionally equivalent? Could someone explain to me the mechanics of pointers to functions and such?</p> <p>One last question, would it work to, inside a for loop generating multiple threads, initialize an <code>int samplearg</code> for thread unique values then pass it to <code>pthread_create(...)</code>? I understand <code>samplearg</code> would be in scope inside the <code>threadfunc</code>, I'm just checking in case somehow C doesn't follow typical scope rules--since <code>samplearg</code> is created inside the <code>for()</code> loop and would typically go out of scope after the iteration of the <code>for()</code> loop, and the actual variable itself is being passed not the value. I'd test myself but there might be a point you could enlighten me on, and developing on a remote linux machine is a bit cumbersome for me.</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.
    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