CreateSemaphore() ?

This is a discussion on CreateSemaphore() ? within the Windows Programming forums, part of the Platform Specific Boards category; Can some one please explain to me what is the CreateSemaphore function, because the msdn defiition is so lame!!! I ...

  1. #1
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    CreateSemaphore() ?

    Can some one please explain to me what is the CreateSemaphore function, because the msdn defiition is so lame!!!

    I know I'm suppost to use it with threading, but why? what are counts?



    Thank you.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  2. #2
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    You just want to know what it does or do you want to know how to use it?? Explain.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  3. #3
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    I know how, but what does it do, and why do need it?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  4. #4
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    semaphores are useful in many situations, a classic example:
    process a wants resource X and Y, process b wants resource X and Y
    process a has resource X, process b has resource Y
    it's a resource deadlock, using a semaphore can prevent this sort of thing from happening.
    Semaphores can also be used to keep track of how many instances of your app is open, there are a lot of uses.

    Here is an ok definition that may help

  5. #5
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    so it allows the threads to use the same resources?
    isn't there some sort collision here, like process A update a value or something like it...
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    If you read through my basic MT tut you will see me using a thing called a Critical Section object. It is used for synchronisation between threads. A Semaphore can be used in exactly the same way, it is a synchronisation object.

    It is more sophisticated then a Critical Section, but in essence, allows safe sharing of a resource between threads or processes.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by adrianxw
    It is more sophisticated then a Critical Section, but in essence, allows safe sharing of a resource between threads or processes.
    what kind of resources are we talking about here?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Physical devices, virtual devices, areas of memory, (i.e. shared variables etc.). Just about anything that two or more threads could possibly want to use during the execution of a process.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #9
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    let say i have a global variable, it will be shared by the threads... but can all of the threads just read from it, or write to it as well?

    and is there a way to block some resources from a thread?
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  10. #10
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Reading from a global variable is safe. If your threads write to a shared resource, such as a global variable, then you have to use synchronisation to ensure things work. If you don't, you will get into situations where it all works most of the time, but every now and again, it fails, amd it won't tell you why.

    If you didn't read my little tutorial, I suggest you do so. The concepts developed there apply generally to MT programming.

    >>> is there a way to block some resources from a thread?

    Don't use them.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  11. #11
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Can I use the same Semaphore for more than one thread? Or should I create a Semaphore per thread?

    BTW, once I call ReleaseSemaphore(), can I re-OpenSemaphore() the Semaphore object without the need of calling for CreateSemaphore() again?

    thank you.
    Last edited by Devil Panther; 08-02-2005 at 10:40 AM.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. about CreateSemaphore()
    By fnoyan in forum Windows Programming
    Replies: 1
    Last Post: 05-08-2006, 09:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21