04-20-2007 01:33 PM - edited 04-20-2007 01:33 PM
Message Edited by jmpugh on 03-10-2007 01:33 PM
04-20-2007 01:45 PM - edited 04-20-2007 01:45 PM
Message Edited by jmpugh on 04-20-2007 01:46 PM
04-20-2007 02:07 PM
04-20-2007 02:20 PM
04-20-2007 02:20 PM
And if Lynn's suggestion does not do it for you here is an outline of an Action Engine that will do the job.
Init
Create an array of 40 numbers along with a similar sized array of booleans. THe numbers are your numbers and the booleans track wich values are in use.
Get Number
Searches the boolean array for the first false (indicating not in use) and then use the returned index to find the number to return. Set the boolean falg true so others do not use it. If the returned index is less than 0 then no numbers are available. Return a flag indicating such.
Return Number
Uses the value passed by the caller to serach the array of number and use the index of that element to clear the boolean "in use" flag for that number. If the boolean was already false, there was a program erro in the caller.
Have fun!
Ben
04-20-2007 02:30 PM
04-20-2007 02:37 PM
The Action Engine link expalins thtat the "semaphores behaviour" is built into the AE.
Ben
04-20-2007 02:46 PM
04-20-2007 03:10 PM
Thank you Marc! I get it now, since the vi won't be re-entrant it can't run twice at the same time. Heh. Very clever!
It's also always educational AND entertaining to see how much neater and less "going around your butt to get to your elbow" a more experienced LabVIEWer's code is. The one I was attempting to create made use of several more array functions.
The reason I like the semaphore idea is I will have to check first to see if that space (in 1-40) is availible before moving to it. If it is already taken, I'll simply need that "player" to wait for it to open. I see how the action engine ensures only one number at a time is claimed, but I would still have to use a semaphore to keep other "players" out of that space until the occupying player has moved on. Correct?
Thanks to all!
-- Jason
04-20-2007 03:45 PM
Not necessarily. Like I said, my example was just to get you started. You could add actions for 'enter' and 'leave'. In the enter action, if that array value is already true, you don't do anything. You could even return a boolean to represent an error trying to enter a space. When a player calls the action engine with 'enter', he sees there is already another player there so he has to wait.
You could have a queue of players that want to move. Each element could be the player and the space he wants to go to. Enqueue a player when he wants to move, then try the move, if that space is occupied, move the player to the back of the queue. There are a lot of possibilities for this. It sounds like a very good exercise to learn some very good programming techniques.
A semaphore might be helpful because it can wait as long as you want and enter immediately when it becomes available, but I think it will be more trouble than it's worth.