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.
Printable View
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.
You just want to know what it does or do you want to know how to use it?? Explain.
I know how, but what does it do, and why do need it?
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
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...
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.
what kind of resources are we talking about here?Quote:
Originally Posted by adrianxw
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.
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?
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.
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.