Semaphores, need advice on implementation.

This is a discussion on Semaphores, need advice on implementation. within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to control the movement of a graphics image across the screen, and was hoping of some ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Semaphores, need advice on implementation.

    Hi,

    I am trying to control the movement of a graphics image across the screen, and was hoping of some advice on the matter.

    Code:
    for(;;)
    	{
    		Sleep(1);
    
    		switch(train1.move())
    		{
    		case  BRIDGE_APPROACH:
    
    			msgsem.wait();
    			display_string("Train 1 arrived at Bridge\n",2);
    			msgsem.signal();
    			onbridge.signal();
    
    			break;
    
    		case  LEAVING_BRIDGE:
    
    			msgsem.wait();
    			display_string("Train 1 left the Bridge\n",2);
    			msgsem.signal();
    			onbridge.wait();
    
    			break;
    As the image crosses the bridge, I use the first line in bold to increase the semaphores value by one, and when the image leaves the bridge, I use the second line in bold to decrease the semaphores value by one.

    Currently this is my only use of the semaphore, but when i run the code I recieve the following error:

    Semaphore::signal
    train 1 thread failed in signal semaphore (onbridge sem)
    tried to increment semaphore that already was at it's maximum value.
    I am trying to limit the number of images on the bridge to 2, and I understood that semaphores can hold a value of > than 1, so am confused why it is saying the semaphore was already at it's maximum value.

    I wanted to solve the problem by reading the semaphores value, if IF > 2, stop other images from crosing the bridge.

    If anyone can offer some advice on this, I would be most greateful.

    Thanks!

  2. #2
    Banned
    Join Date
    Jan 2009
    Posts
    30
    Dear Swerve,

    I have reviewed your code carefully and cannot recognize precisely which API you are using for your sample code. I have done some googling but realize it is probably much more beneficial to us both if you simply tell me what software you are incorporating into your project. I appreciate all auxilary information you can give.

    -sphynx

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You're using the semaphore the wrong way around. You need to wait on it when you enter the bridge and signal it when you leave.

    That said, unless you use a separate thread for every train (which is a bad idea), waiting on a semaphore is the wrong way to control bridge access.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rand() implementation
    By habert79 in forum C Programming
    Replies: 4
    Last Post: 02-07-2009, 12:18 PM
  2. semaphores
    By Dr Spud in forum C Programming
    Replies: 7
    Last Post: 09-22-2007, 12:45 PM
  3. Semaphores Problems
    By mhelal in forum Linux Programming
    Replies: 2
    Last Post: 05-06-2007, 10:36 PM
  4. implementation file
    By bejiz in forum C++ Programming
    Replies: 5
    Last Post: 11-28-2005, 12:59 AM
  5. girl friend advice (prob. the wrong place)
    By B0bDole in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 10-22-2004, 06:38 PM

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