getting semaphores to work

This is a discussion on getting semaphores to work within the C++ Programming forums, part of the General Programming Boards category; Code: #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; ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    26

    getting semaphores to work

    Code:
    #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);
            srand((unsigned)time(NULL));
        	for(i=0; i<numThreads; i++)
        	{                                 
        		pthread_create(&myThreads[i],NULL,producer,(void *)i);
        	}
        	for(i=0; i<numThreads; i++)
        	{
            	pthread_join(myThreads[i],NULL);
        	}
        	sem_destroy(&mutex);
        	return(0);
    }
    
    void *producer(void *ptr)
    {
    	int x;
    	int b;
    	b = (rand() & 255);
    	for(x=0; x<b; x++)
    	{
    		if( (rand() & 1) == 0)
    		{
    			handler(ptr);
    		}
    	}
    	return 0;
    }
    
    void handler(void *ptr)
    {
        	sem_wait(&mutex);
        	counter++;
        	cout<<"Thread "<<ptr<<" New Counter Value: "<<counter<<endl;
    	sem_post(&mutex);
    }
    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
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    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
    Location
    Boulder
    Posts
    1,728
    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
    Posts
    4,649
    Do you have "-lpthread -lrt" in your makefile to link with the pthread and realtime libraries?

    gg

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

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    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, 03:48 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21