multithreading - Reusable Barrier Algorithm -


i'm looking reusable barrier algorithm book "the little book of semaphores", available here http://greenteapress.com/semaphores/downey08semaphores.pdf

the puzzle on page 31 (basic synchronization patterns/reusable barrier), , have come 'solution' (or not) differs solution book (a two-phase barrier).

this 'code' each thread:

# n = 4; threads running # semaphore = n max., initialized 0 # mutex, unowned.  start:     mutex.wait()         counter = counter + 1         if counter = n:             semaphore.signal(4) # add 4 @ once             counter = 0     mutex.release()     semaphore.wait()         # critical section     semaphore.release() goto start 

this seem work, i've inserted different sleep timers different sections of threads, , still wait threads come before continuing each , every loop. missing something? there condition fail?

i've implemented using windows library semaphore , mutex functions.

update:

thank starblue answer. turns out if whatever reason thread slow between mutex.release() , semaphore.wait() of threads arrive semaphore.wait() after full loop able go through again, since there 1 of n unused signals left.

and having put sleep command thread number 3, got result http://pastebin.com/raw.php?i=ffxccmz3 1 can see thread 3 missed turn first time, thread 1 having done 2 turns, , catching on second turn (which in fact 1st turn).

thanks again input.

one thread run several times through barrier while other thread doesn't run @ all.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -