Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm writing a class that will handle thread synchronisation.

It would have a begin method which would increment a counter and an end method that would de-increment a counter. It'll have another method which will block while the counter is not zero, then perform an action while holding the lock.

Its a bit like the opposite of a semaphore, because it's unlocked at zero, and can get locked multiple times, only becoming unlocked again once it's been unlocked an equal number of times.

Is there a name for this pattern?

It's a class based on the accepted answer of my question here

share|improve this question

closed as primarily opinion-based by durron597, Ixrec, Snowman, gnat, enderland Aug 31 '15 at 20:53

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

Unless I'm misreading, how is this not a counting semaphore? – Thomas Owens Aug 3 '11 at 13:38
My understanding about semaphores is that they allow multiple (a known number of) threads to access a resource, but i want to allow other threads calling begin and end to increase/decrease a counter that gets unlocked if it's zero. – George Duckett Aug 3 '11 at 13:40
Edited with the link to basic idea. – George Duckett Aug 3 '11 at 13:43
You must know the number of times it can get locked. Otherwise, you have no method of determining when a new thread must wait instead of incrementing the counter and having the resource. – Thomas Owens Aug 3 '11 at 13:44
Certain operations are allowed on the resource concurrently (that would be ran between a call to begin, and a call to end), however another operation must only get ran when no other operations are being ran (that's what will run when the counter is zero). – George Duckett Aug 3 '11 at 13:50
up vote 1 down vote accepted

It sounds similar to a read write lock. Allow multiple readers at one time but to write you have to be the only thing holding the lock. Replace "read" with behavior #1 and "write" with behavior #2.

share|improve this answer
That's exactly what this is. Is there a pattern name, or is it just a read/write lock? – George Duckett Aug 3 '11 at 14:33
@George the pattern is just called read/write is one reference, i'm sure you can google others as well – user29776 Aug 3 '11 at 15:38

I would say you have implemented a modification of the producer-consumer-constellation, where the consumer runs a method until a store is empty. Your store is the counter. Your consumer is MethodB and your producer is MethodA.

It fit's not 100 percent, because you don't block MethodB but you block in BlockedMethod. But I think this is an appropiate point of view.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.