|
-
June 29th, 2010, 10:54 PM
#1
Help with Semaphore
Usually semaphore is used to allow a certain number of threads to access the resources at the same time. So it is possible for two or more than two threads to access the same resource at the same time, which means there is potential danger of race condition. I wander how semaphore manages to synchronize the access of the resource? Thanks for your inputs.
-
July 18th, 2010, 12:07 AM
#2
Re: Help with Semaphore
Yes, you can easily create a race condition with pretty much anything. You need to provide more information though. If a race condition is a possibility I'd opt to use a mutual exclusion technique instead.
I'm also curious if you had a chance to look at this example of using semaphores?
-
July 18th, 2010, 03:24 PM
#3
Re: Help with Semaphore
Let me make myself clear based on the semaphore example you refer to in your post. Suppose there is 12 threads and max count for semaphore object is 10. Also there is a single resource called R. So in this condition, at most 10 threads might access resource R at the same time, right? Obviously there is race condition, right? Okey, then my question is how'd we do to avoid race condition in this condition. Hopefully, I make myself clear this time. Thanks.
 Originally Posted by ahmd
Yes, you can easily create a race condition with pretty much anything. You need to provide more information though. If a race condition is a possibility I'd opt to use a mutual exclusion technique instead.
I'm also curious if you had a chance to look at this example of using semaphores?
-
July 18th, 2010, 03:39 PM
#4
Re: Help with Semaphore
 Originally Posted by LarryChen
Suppose there is 12 threads and max count for semaphore object is 10.
It will let only 10 of them through and the rest will remain in the waiting state.
You have to realize that semaphore itself doesn't guarantee a prevention of a race condition, it's simply a technique to limit the number of threads that can perform a particular task. In most cases to prevent a race condition you will need a mutex or another synchronization object to allow access to the resource on a singular basis.
-
July 18th, 2010, 03:51 PM
#5
Re: Help with Semaphore
So in the scenario of using semaphore, we always assume even though there is multiple threads accssing the data at the same time but they are "reading" the data so it won't cause any race condition. Am I right?
 Originally Posted by ahmd
It will let only 10 of them through and the rest will remain in the waiting state.
You have to realize that semaphore itself doesn't guarantee a prevention of a race condition, it's simply a technique to limit the number of threads that can perform a particular task. In most cases to prevent a race condition you will need a mutex or another synchronization object to allow access to the resource on a singular basis.
-
July 18th, 2010, 04:12 PM
#6
Re: Help with Semaphore
 Originally Posted by LarryChen
So in the scenario of using semaphore, we always assume even though there is multiple threads accssing the data at the same time but they are "reading" the data so it won't cause any race condition. Am I right?
In most cases you will rarely need a semaphore. Again, semaphore by itself will not eliminate a race condition, it all depends on the type of code you're applying it to.
For example, if you have 9 threads reading from the file F and 1 thread writing into the file F, then restricting access to all 10 of them with a semaphore will be like not using it at all.
An example of a correct use for a semaphore would be this. Say, you have a service that allows clients to read from a file but you want to restrict the number of clients that can do it at the same time (say, for the purpose of CPU load marshaling). In that case you use semaphore to allow only N clients to read from a file at the same time, while keeping the rest of them waiting for their turn.
ALSO NOTE that the example above applies only to reading from a file. With writing at hand you will also need to provide a mutually exclusive access to that file.
-
July 19th, 2010, 04:44 PM
#7
Re: Help with Semaphore
 Originally Posted by LarryChen
So in this condition, at most 10 threads might access resource R at the same time, right? Obviously there is race condition, right?
Wrong. A race condition is a situation in which multiple threads access the same resource and at least one of those threads performs a write operation. If all threads are reading there is no race condition and, hence, no need for synchronization.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|