Click to See Complete Forum and Search --> : thread resource sharing


manish_velankani
July 22nd, 2005, 05:49 AM
Hello All,

A basic question in threads. This is one of the topic which has always perplexed me. First of all what is the most important difference between mutex and semaphore. I mean how and in which scenario they should be used. And what should be the measures to keep the sanity of shared memory being used intact.

Thanx in advance
MS

Kheun
July 22nd, 2005, 06:13 AM
Semaphore can be used to limits the number of simultaneous access to shared resource(s). For example, you can use it to limit your program to only create 6 windows. As for mutex, it is mutually exclusively to only allow an access at a time. In other words, mutex is a specialization of the semaphore.

SuperKoko
July 22nd, 2005, 11:56 AM
Semaphore can also be used to allow the access of resources produced by a thread.
For example a thread could get user input and push strings in a queue (the queue being thread-safe, using critial sections).
After pushing each string, the thread calls ReleaseSemaphore.
Some other threads waits for this semaphore, and get the strings from the queue, and process them, then loop to process other user input.
So, we can have a thread pool.
In that case, the maximum count of the semaphore should be generally set to numeric_limits<LONG>::max().

Andreas Masur
July 22nd, 2005, 02:38 PM
[ Redirected thread ]

mehdi62b
July 23rd, 2005, 05:06 PM
semaphore is a synchronization object that maintains a count between zero and a specified maximum value(N)

so the semaphore has a counter associated

When a semaphore is released the counter is incremented.
When a semaphore is requested if it's value is zero the caller is blocked until someone increments the semaphore if it's greater then zero the counter is decremented
mutex is like a semaphore

mutex is a semaphore whose N is 1
so in a mutex when a thread waits for it the count becomes zero so other threads blocks until the release of the mutex by that thread
a semaphore is used for controling the access of the resources if you set it to 5..just it lets the threads could deal with the resource just five times(or less) at a specific time.
Semaphore can also be used to allow the access of resources produced by a thread.
For example a thread could get user input and push strings in a queue (the queue being thread-safe, using critial sections).
After pushing each string, the thread calls ReleaseSemaphore.
Some other threads waits for this semaphore, and get the strings from the queue, and process them, then loop to process other user input.
So, we can have a thread pool.
In that case, the maximum count of the semaphore should be generally set to numeric_limits<LONG>::max().I didn't get your meaning about the last line of your post..I can't get why it shoud be set to the maximum number??
I think you meant a simple buffering...in that case I think there should be two semaphores,one for the producer and another for consumer,the count of the first one could be the lenght of the queue(buffer) and the count of the consuming semaphore is 0
this is a pesudo code should clarify my meaningItemProducer function
{
producersemaphore.wait();
//put the string in the queue
consumingsemaphore.release();
}
ItemConcumer function
{
consumingsemaphore.wait();
//get the string from the queue
producerersemaphore.release();
}