counting semaphore?
sssidney
Posts: 64
Im trying to implement a counting semaphore to periodically synch 2 cogs.
The main routine launches the code into the cogs that is basically doing this:
So does this mechanism work or is this a race condition?
The main routine launches the code into the cogs that is basically doing this:
VAR long semCount CON SEM_COUNT_LIMIT = 2 PUB testCog1 repeat 'do something useful here ' counting semaphore or race condition? semCount += 1 repeat until ((semCount // SEM_COUNT_LIMIT) == 0) PUB testCog2 repeat 'do something useful too. ' counting semaphore or race condition? semCount += 1 repeat until ((semCount // SEM_COUNT_LIMIT) == 0)
So does this mechanism work or is this a race condition?
Comments
John Abshier
Why not just have a flag?
1) When true testCog1 runs. testCog1 sets the flag false after whatever number of iterations. Then waits for the flag to go true again.
2) When false testCog2 runs. testCog2 sets the flag true after whatever number of iterations. Then waits for the flag to go false again.
I use this all the time. As long as there are only two cogs in the game it works fine.
BTW, the counters (both have to be longs) will eventually roll over. That's why the test condition cannot be mycount > othercount.
-Phil