CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2000
    Posts
    1,471

    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.

  2. #2
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    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?

  3. #3
    Join Date
    Jul 2005
    Posts
    1,030

    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.
    Quote Originally Posted by ahmd View Post
    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?

  4. #4
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Help with Semaphore

    Quote Originally Posted by LarryChen View Post
    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.

  5. #5
    Join Date
    Jul 2005
    Posts
    1,030

    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?
    Quote Originally Posted by ahmd View Post
    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.

  6. #6
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Help with Semaphore

    Quote Originally Posted by LarryChen View Post
    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.

  7. #7
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Help with Semaphore

    Quote Originally Posted by LarryChen View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured