Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Semaphore  RSS feed

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I know Mutex is binary Semaphore,And we have two kind of semaphore:binary and counter semaphore.

The question is:
Does counter semaphore use mutex in order to lock?

How semaphore lock ?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a bit advanced for the beginners forum. We have a forum dedicated to threads and synchronization. I'll move this there for you.
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't looked at the Sun source code, but I have always assumed that the counter concurrency controls are properly implemented, which means they would have to have a mutex protecting them.

There are tons of them in the standard JDK, check out java.util.concurrent
in the javadocs
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Semaphore class was added with Java 5. If I remember correctly, this class does not use Java synchronization. It uses the atomic variables stuff, under the covers. Java 5 also added a mutex (the Lock class), this class also uses atomic variables instead of synchronization.

Henry
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry, did they base the JDK1.5 stuff on your code? I bought the book and implemented the code back when the first edition came out.

Of course, I changed the names to semiphore with P and V just for old times sake
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pat Farrell:
Henry, did they base the JDK1.5 stuff on your code? I bought the book and implemented the code back when the first edition came out.


No. When Java 5 was released, we basically had to pretty much rewrite most of the book for the third edition. It just wasn't pragmatic to teach a topic, using externally written tools, when Java had them built in.

Henry
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
My question is not about semaphore class in JAVA!

In generally,Does Semaphore use mutex in order to lock shared resource?
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In generally,Does Semaphore use mutex in order to lock shared resource?


Well, since you are *not* talking about Java, then the answer is... it depends. I am sure there are Semaphore implementations that use mutexes, under the covers -- but you will have to look into the implementation that you are concerned with yourself.

Henry
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by abalfazl hossein:
My question is not about semaphore class in JAVA!


Sorry, but since this is JavaRance, and in the Java section, its pretty easy to assume you were asking about Java.

I'm now not sure what you are asking, as how its done is an implementation details, which is certain to vary with each language/implementation
 
Henry Wong
author
Sheriff
Posts: 23275
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmmm.... I just thought of another way of interpreting the question.

If by "mutex" you mean pessimistic locking, then the answer is no. A mutex makes the assumption that the action must be mutually exclusive, the locking is pessimistic.

However, it is possible to use optimistic locking to implement a semaphore. This means that the threads assume that the action will work in parallel, and have to deal with a collision (failure), only when it happens. In fact, that is how it is done in Java 5.

So... No. A Semaphore doesn't require that it is to be implemented with mutually exclusive locks under the covers. It may be done that way, but it doesn't require it.

Henry
[ October 14, 2007: Message edited by: Henry Wong ]
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is anybody else here impressed by Henry's knowledge of concurrency and semaphores?



-Cameron McKenzie
 
Nicholas Jordan
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Cameron McKenzie:] Is anybody else here impressed by Henry's knowledge of concurrency and semaphores ?

Yes, and did you see his heart-ripping admission in the Jobs Discussion ?


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!