Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with semaphores and sem_wait()
    primarykey
    data
    text
    <p>I have a queue structure that is being used by several pthreads. The threads are supposed to dequeue from the queue if it's not empty and then do their business.</p> <p>I initially had this set up as a while loop where the threads checked whether the queue was empty using a mutex_lock. Unfortunately this slowed my program down to a crawl.</p> <p>I tried to implement a semaphore as the "count" variable of my queue, but unfortunately I'm running into a segfault when I try and call sem_wait(). I've found the gdb and semaphore.h don't play well together, so I'm really at a loss. I may be making a novice mistake, so any help or suggestions would be appreciated.</p> <p>Queue structure:</p> <pre><code>typedef struct { int q[QUEUESIZE+1]; int first; int last; sem_t count; } queue; </code></pre> <p>Here is the initialization of it:</p> <pre><code>queue *CreateQueue(void) { queue *q; q = (queue*)malloc(sizeof(queue)); if (q == NULL) return NULL; q-&gt;first = 0; q-&gt;last = 0; sem_init(&amp;(q-&gt;count),0, 0); } </code></pre> <p>And I make sure that I call:</p> <pre><code> queue *q; q = CreateQueue(); </code></pre> <p>before any threads are created.</p> <p>Here is the call that seg faults</p> <pre><code>void *ThreadWait(void *t) { while(1) { sem_wait(&amp;(q-&gt;count)); //THIS SEGFAULTS ThreadFun(); //this is the function the thread would go to to do all the work } } </code></pre> <p>I'm hoping this is just a simple mistake on my part that I can't see right now.</p> <p>Thanks in advance.</p> <p>EDIT: to add some clarifying code</p>
    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.
 

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