semaphore or mutex
I read somewhere:
"With a mutex, only one thread can own it at any given moment. With a semaphore, multiple threads can own it at a time."
I don't understan why to allow multiple threads own a semaphore at the same time. Isn't that a new potential synchronization problem?
If someone explain me in which situations to use mutex and in which situations to use semaphores?
I had an application which ran on some very different computers. Some had masses of memory and fast CPU's others were "challenged" in the capability arena.
The application processed files received from a wireless LAN. The processing of each file was atomic, i.e. it idn't use shared data.
By experiment, I discovered that there was an optimum number of threads per machine. Machine A would get through a 1000 files in the shortest time with 150 threads running. Machine B had the best performance with 60 threads running. In the end, I developed an algorithm that "tuned" the number of threads running as the process ran. The way I enforced the thread count limit was to create threads and have them immediately wait for a semaphore which had the lMaximumCount value set to the calculated value. If there were less than that number of threads running, the new thread ran at once, if there were already enough threads running, the threads queued themselves waiting for other threads to finish.
I'm really interested in knowing how you had it 'tune' the lMaximumCount value. Care to explain?