Thread: getting semaphores to work

  1. #1
    Registered User
    Join Date
    Sep 2004

    getting semaphores to work

    #include <iostream>
    #include <sys/types.h>
    #include <unistd.h>
    #include <pthread.h>
    #include <semaphore.h>
    #include <signal.h>
    #include <stdio.h>
    #include <stdlib.h>
    using namespace std;
    void *producer(void *ptr);
    void handler(void *ptr);
    sem_t mutex;
    int counter=0;
    int main()
    	int i; int numThreads=1;
    	pthread_t *myThreads;
    	myThreads=new pthread_t [numThreads];
    	sem_init(&mutex, 0, 1);
        	for(i=0; i<numThreads; i++)
        		pthread_create(&myThreads[i],NULL,producer,(void *)i);
        	for(i=0; i<numThreads; i++)
    void *producer(void *ptr)
    	int x;
    	int b;
    	b = (rand() & 255);
    	for(x=0; x<b; x++)
    		if( (rand() & 1) == 0)
    	return 0;
    void handler(void *ptr)
        	cout<<"Thread "<<ptr<<" New Counter Value: "<<counter<<endl;
    I am trying to compile the following code under NetBSD, using the g++ compiler. Get the following errors:
    /var/tmp//ccpSzRS9.o(.text+0x39): In function `main':
    : undefined reference to `sem_init'
    /var/tmp//ccpSzRS9.o(.text+0xb1): In function `main':
    : undefined reference to `pthread_join'
    /var/tmp//ccpSzRS9.o(.text+0xc8): In function `main':
    : undefined reference to `sem_destroy'
    /var/tmp//ccpSzRS9.o(.text+0x135): In function `handler(void*)':
    : undefined reference to `sem_wait'
    /var/tmp//ccpSzRS9.o(.text+0x1a3): In function `handler(void*)':
    : undefined reference to `sem_post'

    Any ideas on how to fix these errors? I think it something with the libraries.

  2. #2

    From my past coding experience, I have not used semaphores in C++. Therefore I may not be able to help you as much as possible.

    Though, here are some useful links on using Semaphore:

    I will continue my research on Semaphores to see if any more information arises.

    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Which header file are those functions supposed to be in, and have you opened up the files themselves to see if in fact they are there?

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Do you have "-lpthread -lrt" in your makefile to link with the pthread and realtime libraries?


  5. #5
    Registered User
    Join Date
    Sep 2004
    that did the work, thanks

  6. #6
    Registered User
    Join Date
    Dec 2004
    Incidentally, why use semaphores? A mutex would be more efficient.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  3. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM