CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2008
    Posts
    31

    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
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Semaphores, need advice on implementation.

    A Semaphore starts with an initial value. As items request the resource, the number decrements, and block the thread, when the thread is done with the semaphore its value is incremented, and pending request is allowed to proceed.

    The ONLY way to have the number increase above the initial value is by calling release, without actuallu having a value aquire....
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: Semaphores, need advice on implementation.

    I'm not sure why you think that you need two semaphores. You really only need one semaphore initialized with the value representing how many users can use the bridge at one time. Whenever something wants to use the bridge you call wait. Whenever something stops using the bridge you call signal. if there are already 2 users then the 3rd will wait until one of the others signals that they are off of the bridge. Then you have to start thinking about how long to wait and whether you want to use a conditional wait to determine if the next user really wants to wait or attempt to use a different resource (if others are available).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured