Note that there are some explanatory texts on larger screens.

plurals
  1. POpthread_t pointer as argument of pthread_create
    primarykey
    data
    text
    <p>The first argument of pthread_create is a pthread_t pointer. In the <em>hello program</em> below, if the first argument is a pointer to pthread_t (<code>pthread_t*</code>) instead of a pthread_t (<code>pthread_t</code>) the program ends with <code>Segmentation fault</code>...<strong>why?</strong> <br/> <br/></p> <p>I don't remember seeing <code>pthread_t*</code> as the declared type of the first argument of <code>pthread_create</code>. <br/> And chapter 2 of Butenhof's book <em>Programming with POSIX Threads</em> says: </p> <blockquote> <p>To create a thread, you must declare a variable of type <code>pthread_t</code> [not <code>pthread_t*</code>]. </p> </blockquote> <p>But <a href="http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_create.html" rel="nofollow">according to the specification</a> the first argument of <code>pthread_create</code> is a pointer to <code>pthread_t</code>, so why the segmentation fault?</p> <p><br/> <hr> Segmentation fault</p> <pre><code>pthread_t* thr; pthread_create(thr, NULL, &amp;hello, NULL); </code></pre> <p><br/> <hr> Runs OK</p> <pre><code>pthread_t thr; pthread_t* pntr = &amp;thr; pthread_create(pntr, NULL, &amp;hello, NULL); </code></pre> <p><br/> <hr> hello program:</p> <pre class="lang-c prettyprint-override"><code>#include &lt;pthread.h&gt; #include &lt;stdio.h&gt; void * hello(void *arg){ printf("Hello\n"); pthread_exit(NULL); } int main(int argc, char **argv){ pthread_t thr = 1; pthread_create(&amp;thr, NULL, &amp;hello, NULL); pthread_join(thr, NULL); return 0; } </code></pre> <hr> <p>pthread_create prototype:</p> <pre><code>int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void*), void *arg); </code></pre>
    singulars
    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.
 

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