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.