Note that there are some explanatory texts on larger screens.

plurals
  1. POThread calling enqueue function in C
    primarykey
    data
    text
    <p>I have:</p> <pre><code>struct elem { data d; struct elem *next; }; typedef struct elem elem; struct queue { int cnt; elem *front; elem *rear; }; void enqueue(data d, queue *q); void enqueue(data d, queue *q) { elem *p; p = malloc(sizeof(elem)); p -&gt; d = d; p -&gt; next = NULL; if (!empty(q)) { q -&gt; rear -&gt; next = p; q -&gt; rear = p; } else q -&gt; front = q -&gt; rear = p; q -&gt; cnt++; } </code></pre> <p>which would be call:</p> <pre><code>int main(){ struct queue Q; initialize(&amp;Q); //init the queue enqueue( 10000, &amp;Q); return 0; } </code></pre> <p>and some thread creation like: </p> <pre><code> #include &lt;pthread.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #define NUM_THREADS 5 /** * * */ pthread_t threads[NUM_THREADS]; long t; for(t=0;t&lt;NUM_THREADS;t++){ pthread_create(&amp;threads[t], NULL, enqueue, (void *)t); } </code></pre> <p>How should I modify the enqueue function so in the pthread_create each thread call </p> <pre><code>enqueue( variable, &amp;Q); </code></pre> <p>(I am doing a lock free queue, and already has the logic, but I am stuck in How would each thread call enqueue function...)</p> <p><strong>--EDIT--</strong></p> <p>I am doing the answer proposed and getting:</p> <pre><code> queue.c: In function ‘main’: queue.c:130: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type /usr/include/pthread.h:227: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)(data, struct queue *)’ </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.
    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