LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   POSIX semaphores acting strangely (https://www.linuxquestions.org/questions/programming-9/posix-semaphores-acting-strangely-244422/)

spuzzzzzzz 10-18-2004 09:10 PM

POSIX semaphores acting strangely
 
I'm writing a threaded program that makes heavy use of POSIX semaphores. I'm running gentoo amd64 with glibc 2.3.4.20040808-r1 (using nptl). The program is too long to explain in its entireity, but here are three sections:

Code:

if(sem_init(&info->post->avail, 0, info->numbufs) != 0
        || sem_init(&info->post->filled, 0, 0) != 0) {
    fprintf(stderr, "error initialising semaphores in postfilter\n");
    exit_with_error(1);
}
printf("initialised\n");

Code:

printf("about to raise\n");
sem_post(&info->post->filled);
printf("raised\n");

Code:

sem_wait(&info->post->filled);
printf("lowered\n");

These sections of the code are the only sections that touch info->post->filled. Since info->post->filled gets initialised to zero, it should not lower until after it has been raised, right? But this is the output (from within gdb), with *** next to the relevant bits:
Code:

[Thread debugging using libthread_db enabled]
[New Thread 182921540368 (LWP 18332)]
Initialising prefilter...
Initialising fft...
Initialising postfilter...
initialised                        ***
Initialising drawing...
[New Thread 1082128736 (LWP 18333)]
[New Thread 1090517344 (LWP 18334)]
[New Thread 1098905952 (LWP 18335)]
[New Thread 1107294560 (LWP 18336)]
[New Thread 1115683168 (LWP 18337)]
[New Thread 1124071776 (LWP 18338)]
lowered                            ***
about to raise                      ***
raised                              ***
lowered
about to raise
raised

Can anyone explain this?


All times are GMT -5. The time now is 01:51 PM.