Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why do we need semaphore?

 
azuki ooh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one of the codes I got from my textbook. Can someone help me to understand why is the sem.acquire() and sem.release() necessary when we can keep track of which item is used or unused simply using the getAvailableItem() and markAsUnused()?



>
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

Does that even compile ? I'm suggesting not.

It also looks slightly bugged.


In general the semaphore could allow you to constrain concurrently a list of resources to a (different) given number concurrently in use where, also getAvailableItem in intent
would potentially give you a no none available (eg null) whereas the semaphore would wait on the concurrent users to release i.e. its a blocking call.
So getAvailableItem is a kind of try to get one of a big set of resources but immediately fail if not available but getItem is a try to get one if two many users are working wait till one finishes.

I'd suggest getting the code to compile and run it first.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic