2

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.

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.

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.

Queue structure:

typedef struct {
  int q[QUEUESIZE+1];
  int first;
  int last;
  sem_t count;
} queue;

Here is the initialization of it:

queue *CreateQueue(void)
{
  queue *q;
  q = (queue*)malloc(sizeof(queue));
  if (q == NULL)
    return NULL;
  q->first = 0;
  q->last = 0;
  sem_init(&(q->count),0, 0);

}

And I make sure that I call:

   queue *q;
    q = CreateQueue();

before any threads are created.

Here is the call that seg faults

void *ThreadWait(void *t) {
  while(1) {
    sem_wait(&(q->count));  //THIS SEGFAULTS
    ThreadFun(); //this is the function the thread would go to to do all the work
  }
}

I'm hoping this is just a simple mistake on my part that I can't see right now.

Thanks in advance.

EDIT: to add some clarifying code

6
  • Can you post a bit more complete code. It's not clear if you init and wait on the same sem_t. Also do you have a core file? Stack traces? Commented Mar 29, 2010 at 19:53
  • @Nikolai Thanks, I added more code, don't have a core file or stack trace. Is there a way to grab a stack trace without gdb? Commented Mar 29, 2010 at 20:02
  • Another ambiguity in your sample code is the variable q in ThreadWait(). Is this a global variable? Could the problem be a memory over write? Also, are you using fork() or pthreads? Commented Mar 29, 2010 at 20:11
  • @atlpeg the q in ThreadWait is a global variable. I'm using pthreads, everything was working fine (but slow) with a mutex instead of a semaphore. I've checked, and q is not defined anywhere else in my code, so I'm thinking it's not an overwrite. Commented Mar 29, 2010 at 20:15
  • 1
    @Evan, try gcore to get core from running process, pstack to get a stack trace. Also, you should check the return value of sem_init. Commented Mar 29, 2010 at 20:16

1 Answer 1

3

If CreateQueue in the above example is complete, then it does not seem to be setting your global q variable. It assigns the results to a local variable. But it does not seem to return the variable.

Sign up to request clarification or add additional context in comments.

1 Comment

You are a miracle worker. I just noticed that it didn't return a value. Another reason to compile with -Wall. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.