bug-gdb
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

pthread, sem_post and gdb


From: Pieter Grimmerink
Subject: pthread, sem_post and gdb
Date: Mon, 15 Oct 2001 22:01:00 +0200

Hello,

I have been experiencing a really strange problem with the combination
of pthreads and POSIX semaphores when debugged with gdb 5.0
I'm using gcc 2.95.3, I don't know the version of my pthread library, but it 
came with glibc 2.2.2


I wrote the following small testprogram:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>

sem_t semaphore;

void *threadproc(void *arg)
{
  //while (1)
  //{
      sem_wait(&semaphore);
      printf("thread output\n");        
  //}
  return NULL;
};

int main(int argc, char *argv[])
{
  pthread_t thread;
  sem_init(&semaphore,0,0);
  pthread_create(&thread, NULL, threadproc, NULL);
  sleep(5);
  sem_post(&semaphore);
  sleep(5);
  printf("main output\n");      
  return EXIT_SUCCESS;
}

When I do

gcc main.c -lpthread

and then

gdb a.out
> run

the output stays quiet for 5 seconds (the first sleep(5)), then
I get "thread output" and "main output" immediately following
eachother, while the 'program exited normally' immediately
after printing this output.
The second 'sleep(5)' immediately seems to return.

When I compile the code with debug info, and step through
the code in main(), the execution stalls at sem_post(&semaphore),
probably because the pthread ends.
So I added a while(1) loop in the threadproc, now I can step
through the complete main function, and the second 'sleep(5)'
takes 5 seconds like it should.
(adding the while loop in the threadproc does not have any
effect on the results of the first test)

I have not been able to find anything related to this problem,
but please forgive my ignorance, because this is one of
my first experiences with gdb...

Best regards,

Pieter Grimmerink



reply via email to

[Prev in Thread] Current Thread [Next in Thread]